String temp = " <td align=\"center\"><h1>您查询的IP:113.106.249.14</h1></td>";
String REGEX_EXP = ".*<td align=\"center\"><h1>您查询的IP:(113\\.106\\.249\\.14)</h1></td>.*";
Pattern p = Pattern.compile(REGEX_EXP);
System.out.println(p.matcher(temp).matches());
Matcher matcher = p.matcher(temp);
System.out.println(matcher.group(1));
抛异常:Exception in thread "main" java.lang.IllegalStateException: No match found
原因是用法不对,需要先find,然后group才能有数据,正确写法:
String temp = " <td align=\"center\"><h1>您查询的IP:113.106.249.14</h1></td>";
String REGEX_EXP = ".*<td align=\"center\"><h1>您查询的IP:(113\\.106\\.249\\.14)</h1></td>.*";
Pattern p = Pattern.compile(REGEX_EXP);
System.out.println(p.matcher(temp).matches());
Matcher matcher = p.matcher(temp);
while(matcher.find()){
System.out.println(matcher.group(1));
}