Why does ^\s*$ not match "" with MULTILINE?
<p>I'm supporting this Java application where the devs implemented some filtering based on RegEx. To be as generic as possible, they compile the patterns with the MULTILINE flag.</p>

<p>The other day I noticed something unexpected.
In Java, the pattern <code>"^\\s*$"</code> does not match <code>""</code> with the MULTILINE flag. It does match without that flag.</p>

<pre><code>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());
</code></pre>

<p>This produces the following output</p>

<pre><code>Multiline: false
Non-Multiline: true
</code></pre>

<p>Same results can be seen for <code>matches()</code>:</p>

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

<p>I would expect all cases to match.<br>
In Python, this is the case. This:</p>

<pre><code>import re

print(re.search(r'^\s*$', "", re.MULTILINE))
print(re.search(r'^\s*$', ""))
</code></pre>

<p>gives:</p>

<pre><code>&lt;_sre.SRE_Match object; span=(0, 0), match=''&gt;
&lt;_sre.SRE_Match object; span=(0, 0), match=''&gt;
</code></pre>

<p>In Perl, both cases match as well and I think I remember it being the same for PHP.</p>

<p>I'd really appreciate if someone could explain the reasoning behind the way Java handles this case.</p>