Why does ^ \ s * $ not match "" with MULTILINE?

I support this Java application where developers have implemented some RegEx-based filtering. To be as general as possible, they compile templates using the MULTILINE flag.

The other day I noticed something unexpected. In Java, the pattern "^\\s*$" does not match "" with the MULTILINE flag. It does not match this sign.

 Pattern pattern = Pattern.compile("^\\s*$", Pattern.MULTILINE); Matcher matcher = pattern.matcher(""); System.out.println("Multiline: "+matcher.find()); pattern = Pattern.compile("^\\s*$"); matcher = pattern.matcher(""); System.out.println("No-multiline: "+matcher.find()); 

This creates the following output

 Multiline: false Non-Multiline: true 

The same results can be seen for matches() :

 System.out.println("Multiline: " + ("".matches("(?m)^\\s*$"))); System.out.println("No-multiline: " + ("".matches("^\\s*$"))); 

I expect all cases to be consistent.
In Python, this is so. It:

 import re print(re.search(r'^\s*$', "", re.MULTILINE)) print(re.search(r'^\s*$', "")) 

gives:

 <_sre.SRE_Match object; span=(0, 0), match=''> <_sre.SRE_Match object; span=(0, 0), match=''> 

In Perl, both cases are the same, and I think I remember that it is the same for PHP.

I would really appreciate if anyone could explain why Java handles this case.

+5
source share
1 answer

You pass an empty string to match. With Pattern.MULTILINE , ^ expected to match the beginning of the line, but in Java it might be slightly different:

If the MULTILINE mode MULTILINE activated, then ^ matches at the beginning of the input and after any line terminator , except at the end of the input .

Since the line is empty, the beginning of the input is its end.

Note. If you pass the default flag, but you really want the patterns to match the beginning of the line, you can use \A instead of ^ and \z to end the line instead of $ , which will match the beginning / end of the line even with Pattern.MULTILINE (and even an empty line will pass the test \\A\\s*\\z ).

+3
source

Source: https://habr.com/ru/post/1241082/


All Articles