java 正则

 

 1. 贪婪与惰性:

     .* 这样就是贪婪,.*? 加个问号就是惰性,只匹配到最近的。

2. 分组

   例如

String regPtnStr = "<h4><pstyle=\"width:400px;float:left;height:39px;overflow:hidden;\"><a[^>]*?>(.*?)</a></p>.*?id=\"humorContent_[\\d]*?\"class=\"pic_text\"><p>(.*?)</p></div>";
Pattern p=Pattern.compile(regPtnStr);
Matcher m=p.matcher(orgTitle);
while(m.find()){
System.out.println(m.group(0));
System.out.println(m.group(1));
System.out.println(m.group(2));
}

group是不同的分组,0是全部,1是第一个组即()匹配到的值,2是第二个组即()匹配到的值。

posted @ 2013-01-17 11:59  shenbin23  阅读(139)  评论(0编辑  收藏  举报