1

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?

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Volodymyr Levytskyi
  • 407
  • 1
  • 6
  • 15

2 Answers2

2

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)^.*$");
JonM
  • 1,314
  • 10
  • 14
0

I think that the main reson of this is that you're using the $ symbol in your regex, as it states for an end of a string or, if multiline enabled, for an end of a line. So you simply find the text from the start of a string till the first end of a line in matching text.

I think that the regex in a form of ^.*$ isn't useful at all. What are you trying to achieve? Select whole text - so use .*. If you want to get some text from second line, try to do this:

^.*$.*$

Or try to get your text being splitted by newline char:

String lines[] = String.split("\\r?\\n");
Community
  • 1
  • 1
VMAtm
  • 27,943
  • 17
  • 79
  • 125