I am trying to match a multi line text using Java. When I use the Pattern class with the Pattern.MULTILINE modifier, I am able to match, but I am not able to do so with (?m).
The same pattern with (?m) and using String.matches does not seem to work. I am sure I am missing something, but no idea what.
This is what I tried:
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?
First, you’re using the modifiers under an incorrect assumption.
Pattern.MULTILINEor(?m)tells Java to accept the anchors^and$to match at the start and end of each line (otherwise they only match at the start/end of the entire string).Pattern.DOTALLor(?s)tells Java to allow the dot to match newline characters, too.Second, in your case, the regex fails because you’re using the
matches()method which expects the regex to match the entire string – which of course doesn’t work since there are some characters left after(\\W)*(\\S)*have matched.So if you’re simply looking for a string that starts with
User Comments:, use the regexwith the
Pattern.DOTALLoption:ResultStringwill then contain the text afterUser Comments: