Match Start and End of Line
^abc
and\Aabc
matchabc
at the start of whole stringabc$
andabc\Z
matchabc
at the start of whole string^abc
matchabc
at the start of each lineabc$
matchabc
at the start of each line
cs
_ = new Regex(@"^abcefg$", RegexOptions.Multiline);
1
^
always matches after\n
, so\n^
is redundant$
always matches before\n
, so$\n
is redundant\A\Z
matches empty string and empty string with a single new line\A\z
matches only empty string
TIP
Always use \A
and \Z
instead of ^
and $
when to match start/end of a whole string
Mode modifier
Use (?m)
/(?-m)
to enable/disable multiline
mode in regex literal
Conclusion
\A
and\Z
always match the start and end of a subject string(?-m)^abc
and(?-m)$abc
are equivalent to\Aabc
and\Zabc
\z
matches the end of the subject stringabc\Z
matches before line break whileabc\z
won't match if line break exists afterabc