正则 抽取<div>标签
html:
<div class="mod-head">
<span class="username">
<a data-card-action="xpt=ZWxvbmc1NjdAMTI2LmNvbQ==" data-card="true" href="http://i.sohu.com/p/=v2=ZMd2IjLATmx1WNbvcmNvbQ==/" target="_blank">
峰Top
</a>
</span>
<span class="user-suffix">
</span>
能不高吗?
</div>
想要达到的效果是 取出<div class="mod-head">
我的想法是:
String regEx="<div[^>]*?>";
Pattern pat=Pattern.compile(regEx);
Matcher m=pat.matcher(html);
boolean b=m.matches();
System.out.print(b);
System.out.println(m.group());
但是 报错:
java.lang.IllegalStateException: No match found
------解决方案--------------------下面是我写的代码,参考一下:
Pattern pattern = Pattern.compile("(<div.*?>)");
Matcher matcher = pattern.matcher("<div class=\"red\">很好</div>");
while(matcher.find()) {
System.out.println(matcher.group(1));
}