String stUrl = "<b>本頁主題:</b> java 中正则表达式的问题_百度知道</td><td class= style=></td></tr><tr><td></td></tr><tr class=><br /></th></tr><tr><td></td></tr></table></div><b>3658 點</b></font><br />";Pattern p = Pattern.compile("<b>本頁主題:</b>\\s</td>");
Matcher m = p.matcher(stUrl);
if(m.find())
System.out.println(stUrl.substring(m.start(),m.end()));
else
System.out.println("no");我要匹配 <b>本頁主題:</b>...........</td> 里的字符怎么匹配不成功呢
Matcher m = p.matcher(stUrl);
if(m.find())
System.out.println(stUrl.substring(m.start(),m.end()));
else
System.out.println("no");我要匹配 <b>本頁主題:</b>...........</td> 里的字符怎么匹配不成功呢
String stUrl = " <b> 本頁主題: </b> java 中正则表达式的问题_百度知道 </td> <td class= style=> </td> </tr> <tr> <td> </td> </tr> <tr class=> <br /> </th> </tr> <tr> <td> </td> </tr> </table> </div> <b> 3658 點 </b> </font> <br /> "; Pattern p = Pattern.compile(" <b> 本頁主題: </b>(.*?)</td> ");
Matcher m = p.matcher(stUrl);
if (m.find()) {
System.out.println(stUrl.substring(m.start(), m.end()));
System.out.println(m.group(1));
}
else {
System.out.println("no");
}
}
应该匹配出2个才对,如果没有?那就成贪婪匹配了,他会尽可能匹配最长的,造成
第一个的开头,匹配了第二个的结结尾了。