I just ran into a problem because "^\s*$" doesn't match an empty string!?
Nor does "^$"
Nor does "^" (although just "$" does).
I find if I don't enable multi-line mode, then all of those match, as I'd expect.
Pattern.compile("^").matcher("").find() => true
Pattern.compile("^", MULTILINE).matcher("").find() => false
But I need multi-line mode to make it work the same as cSuneido.
I've tried to find anything in the documentation or on the web to explain this, but haven't had any luck. It doesn't make much sense to me. The documentation says:
By default, the regular expressions ^ and $ ignore line terminators and only match at the beginning and the end, respectively, of the entire input sequence. IfThe only thing I can think of is that it's applying "except at the end of input" even when there is no line terminator. I guess it depends whether you parse it as
MULTILINEmode is activated then ^ matches at the beginning of input and after any line terminator except at the end of input. When in
MULTILINEmode $ matches just before a line terminator or the end of the input sequence.
(matches at the beginning of input) and (after any line terminator except at the end of input)
(matches at the beginning of input and after any line terminator) except at the end of input
To me, the first makes more sense, but it appears to be working like the second.
So far I've been able to handle the differences between Suneido regular expressions and Java regular expressions by translating and escaping the expressions. But that's tricky for this problem. I guess I could turn off multi-line mode if the string being matched doesn't have any newlines. Except I'm caching the compiled regular expressions so I'd have to cache two versions. And it also means an extra search of the string on every match. Yuck.
Of course, my other option is to port cSuneido's regular expression code, rather than using Java's. Ugh.
Backwards compatibility is really a pain!