我试图使用java匹配多行文本.当我使用Pattern带Pattern.MULTILINE修饰符的类时,我能够匹配,但我无法使用(?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?
Run Code Online (Sandbox Code Playgroud)