正则表达式

来源:互联网 发布:ubuntu移除程序 编辑:程序博客网 时间:2024/05/16 04:09
一个或多个字符分解。
    String s = "GET             /index.html HTTP/1.1";//字符串s由“GET”、“/index.html”和“HTTP/1.1”组成,中间有一个或多个空格
    String tt[] = s.split("\\s{1,}");//按照空格分割字符串,多个空格作为一个空格对字符串进行分割
    for(String str: tt)//增强的for循环
        System.out.println("str+++"+str);//输出:GET
一个或多个空格替换成没有    
    String qq = st.replaceAll(" {1,}", "");
匹配图像
String st = "<img src='http://static.l99.com/commentImg/133.gif' />&nbsp;哈哈哈<img src='http://static.l99.com/commentImg/134.gif' />";

        String regEx1 = "(<img.*?/>)";//以<img开头,/>结束
    Pattern pattern1 = Pattern.compile(regEx1);
    Matcher matcher1 = pattern1.matcher(st);
    while (matcher1.find()) {
        System.out.println(matcher1.group());
    }