0

我需要在 html 标记的标题上添加“Java RegEx”,但它不起作用。为什么?

Pattern.compile("\\<td class=\"codeTitle\">(.*)\\</td>");

Matcher m = p.matcher("<td class="codeTitle">Java RegEx</td>");
4

1 回答 1

3

您需要打电话m.find()检查是否找到任何匹配项。如果找到匹配项,您可以使用 访问它们m.group(1)

此外,我认为您忘记转义主题字符串。

Pattern.compile("<td class=\"codeTitle\">(.*?)</td>"); //lazy matching is better in matching html tags

Matcher m = p.matcher("<td class=\"codeTitle\">Java RegEx</td>"); // you didn't escape that

if(m.find()){
  //do something with m.group(1) which contains "Java Regex"
}
else {
  //no matches found
}
于 2013-03-03T11:18:13.450 回答