0

I am sorry I am new in Java. I need to match multiline string in Java which looks like:

meno je povinné pole
priezvisko je povinné pole
heslo je povinné pole
email je povinné pole
email nemá platný formát
musíte súhlasiť s podmienkami

And here is the Pattern to match this string.

Pattern p = Pattern.compile("meno.+heslo", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
Matcher m = p.matcher(alert.getText().toLowerCase());  // text from the example

What is wrong with that? If I test only the first line it works. So I think problem is in the lines.

Čamo
  • 2,352
  • 6
  • 29
  • 66
  • The `.` does not match newlines, try `meno[\s\S]+heslo` instead. – wp78de Feb 19 '20 at 15:45
  • Pattern.MULTILINE does not modify dot behavior? – Čamo Feb 19 '20 at 15:47
  • No, it does not, this is done by the `/s` singleline / dotall flag. – wp78de Feb 19 '20 at 15:50
  • Answer is already here. You answer is not for Java. Question is about Pattern.DOTALL flag – Čamo Feb 19 '20 at 16:02
  • It's still a duplicate. You can find dozens if you search, e.g. https://stackoverflow.com/questions/17824211/how-to-match-regex-over-multiple-lines – wp78de Feb 19 '20 at 16:21
  • Does this answer your question? https://stackoverflow.com/questions/3651725/match-multiline-text-using-regular-expression – wp78de Feb 19 '20 at 16:26
  • My is much more cleaner. I prefer cleaner simple questions before messy specific problems like (?m) modifier ... – Čamo Feb 19 '20 at 17:17

1 Answers1

3

The . will not match the newlines, to fix this use the DOTALL flag when compiling your pattern.

 Pattern.DOTALL

The MULTILINE flag will only do the following:

"In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence."

rhowell
  • 830
  • 5
  • 19