Search code examples
javaregexmultiline

Why doesn't Pattern.MULTILINE match new lines in java regex


enter code hereI don't understand how to capture multiple lines with simple regex:

Pattern pattern2 = Pattern.compile("^.*$", Pattern.MULTILINE);
matcher = pattern
        .matcher("11-41 pm, Oct 20, 2014 - Stef G: Ik zal er ook zij \n ttrrttttkkk");

matcher.find();
System.out.println("group=" + matcher.group());

It outputs:

group=11-41 pm, Oct 20, 2014 - Stef G: Ik zal er ook zij

In output, text after carrage return is missing.

How to avoid this?


Solution

  • The DOTALL option should definitely work for you:

    Pattern pattern2 = Pattern.compile("^.*$", Pattern.DOTALL);
    

    But if it doesn't for some reason you can specify the option in the actual expression like so:

    Pattern pattern2 = Pattern.compile("(?s)^.*$");