我试图使用 java 来匹配多行文本。当我使用带有 Pattern.MULTILINE
修饰符的 Pattern
类时,我能够匹配,但是我无法匹配 (?m).
同样的模式与 (?m)
和使用 String.matches
似乎不工作。
我确定我遗漏了一些东西,但不知道是什么。我不太擅长正则表达式。
我试过了
String test = "User Comments: This is \t a\ta \n test \n\n message \n";
String pattern1 = "User Comments: (\\W)*(\\S)*";
Pattern p = Pattern.compile(pattern1, Pattern.MULTILINE);
System.out.println(p.matcher(test).find()); //true
String pattern2 = "(?m)User Comments: (\\W)*(\\S)*";
System.out.println(test.matches(pattern2)); //false - why?