为什么在 Java 的正则表达式库中 EOL 在下一个命令中找到
Matcher matcher = Pattern.compile( "[\\\\r\\\\n$]+" ).matcher( " where " );
if ( matcher.find() )
{
// found reaction
}
这不是新行正则表达式。您实际上匹配以下字符之一 1 次或多次:\、r、\或. 在中,有一个,因此在字符串中找到了模式。n$wherer
新行正则表达式是\r|\n|\r\n. 在 JAVA 中,您需要转义反斜杠,因此它将是\\r|\\n|\\r\\n.
两个问题:
\rand \n,不是\\rand \\n。在 Java 中,您需要将它们转义一次,因此模式是\\r和\\n。" where "不包含任何回车 + 换行符(只是 \r)。这将找到一个回车+换行:
Matcher matcher = Pattern.compile( "[\\r\\n$]+" ).matcher( " where \n" );
if ( matcher.find() ){
System.out.println("found");
}