[英]Can't undertand why my regex in Java doesn't work
I'm trying to find pieces of text on the webpage I fetch that lay between ' align="left">\\n
" and ' </form>\\n</td>
' substrings. 我试图在我获取的网页上找到位于
align="left">\\n
“和' </form>\\n</td>
'子字符串之间的文本。
I wrote a regex: 我写了一个正则表达式:
(align=\"left\">\\n)(?<part>.*?)(<\/form>\\n<\/td>)
and tested it at https://www.freeformatter.com/java-regex-tester.html where it works just as I need. 并在https://www.freeformatter.com/java-regex-tester.html上对其进行了测试,可以在需要时使用。
But in the Java code it can't find anything. 但是在Java代码中找不到任何东西。
My test code that I'm trying make working: 我正在尝试的测试代码起作用:
String frontPage = "<html>\n<head>\n<title>Hello</title>\n</head>\n" +
"<body>\n<table>\n<tr align=\"left\">\n" +
"<td>Hello \n<form>\n<input type=\"submit\" value=\"ok\">\n" +
"</form>\n</td>\n" +
"<td>World \n<form>\n<input type=\"submit\" value=\"ok\">\n" +
"</form>\n</td>\n" +
"</tr>\n</table>\n</body>\n</html>";
java.util.regex.Pattern p =
java.util.regex.Pattern.compile(
"(align=\"left\">\\n)(?<part>.*?)(<\\/form>\\n<\\/td>)");
java.util.regex.Matcher m = p.matcher(frontPage);
List<String> parts = new ArrayList<>();
while (m.find()) {
parts.add(m.group("part"));
}
if (parts.size() == 0)
System.out.println("No page parts found");
else {
System.out.println("Something matches at least");
}
It finds matches if only first two groups specified, but when I add at least simple (form)
sequence to the last group, it stops matching anything, and I can't even guess why. 如果仅指定了前两个组,它将找到匹配项,但是当我在最后一个组中添加至少简单的
(form)
序列时,它将停止匹配任何内容,我什至无法猜测为什么。
Add DOTALL
to the compile
. 将
DOTALL
添加到compile
。 Like 喜欢
java.util.regex.Pattern.compile(
"(align=\"left\">\\n)(?<part>.*?)(<\\/form>\\n<\\/td>)",
java.util.regex.Pattern.DOTALL
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.