Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression doesn't match empty string in multiline mode (Java)

Tags:

java

regex

I just observed this behavior;

Pattern p1 = Pattern.compile("^$");
Matcher m1 = p1.matcher("");
System.out.println(m1.matches()); /* true */

Pattern p2 = Pattern.compile("^$", Pattern.MULTILINE);
Matcher m2 = p2.matcher("");
System.out.println(m2.matches()); /* false */

It strikes me as odd that the last statement is false. This is what the docs say;

By default, the regular expressions ^ and $ ignore line terminators and only match at the beginning and the end, respectively, of the entire input sequence. If MULTILINE mode is activated then ^ matches at the beginning of input and after any line terminator except at the end of input. When in MULTILINE mode $ matches just before a line terminator or the end of the input sequence. http://docs.oracle.com/javase/1.4.2...

From what I get from this, it should match? The following makes things even more confusing;

Pattern p3 = Pattern.compile("^test$");
Matcher m3 = p3.matcher("test");
System.out.println(m3.matches()); /* true */

Pattern p4 = Pattern.compile("^test$", Pattern.MULTILINE);
Matcher m4 = p4.matcher("test");
System.out.println(m4.matches()); /* true */

So what is this? How do I make sense of this? I hope someone can shed some light on this, would be really appreciated.

like image 584
Wietse Venema Avatar asked Jan 17 '12 14:01

Wietse Venema


1 Answers

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

Since you are at the end of input, ^ can't match in multiline mode.

This is surprising, even disgusting, but nevertheless according to its documentation.

like image 107
Ingo Avatar answered Oct 13 '22 11:10

Ingo