java 正则

 1. 贪婪与惰性:

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

2. 分组

   例如

String regPtnStr = "<h4><pstyle=\"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是第二个组即()匹配到的值。

原文地址:https://www.cnblogs.com/shenbin/p/2864216.html