^ are zero-width meta-characters. Unlike other meta-characters like
. which match one character at a time (unless used with quantifiers), they do not actually match literal characters. This is why
^$ matches an empty string
"", even though the regex (sans delimiters) contains two characters while the empty string contains zero.
It doesn't matter that an empty string contains no characters. It still has a starting point and an ending point, and since it's an empty string both are at the same location. Therefore no matter the order or number of
$ you use, all of their permutations should match the empty string.
Your second case is slightly trickier but the same principles apply.
m modifier (
PCRE_MULTILINE) just tells the PCRE engine to feed in the entire string at one go, regardless of newlines, but the string still comprises "multiple lines". It then looks at
$ as "the start of a line" and "the end of a line" respectively.
"\n" is essentially logically split into three parts:
"" (because the newline is surrounded by emptiness... sounds poetic).
Then these matches follow:
The first empty string is matched by the starting
$^ (as I explain above).
\n is matched by the same
\n in your regex.
The second empty string is matched by the last
And that's how your second case results in a match.
No it is not. Actually, the expression
$^ should never match, because
$ symbolizes the end of a string whereas
^ represents the beginning. But as we know, the end cannot come before the beginning of a string :)
^$ should match an empty string, and only that.
The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, [...]
From the PCRE manpages
Note that, by adding the
$ becomes EOL and
^ becomes BOL, it will match (thanks netcoder for pointing that out). Still, I personally wouldn't use it.