Reputation: 3806
Is the only difference between ^
and \A
the fact that \A
can never match after a line break? (even in multi-line mode)
The PCRE man page says:
^ assert start of string (or line, in multiline mode)
...
\A matches at the start of the subject
Thanks!
Upvotes: 5
Views: 1496
Reputation: 103874
If you have this as your target or subject string:
Line 1\n
Line 2\n
Line 3\n
The regex /^Line/gm
will match all three lines. The ^
anchor matches at the first part of the string or after a logical CR/LF if the /m
flag is present.
The regex /\ALine/gm
will only match the first line no matter what. The \A
assertion only matches at the absolute beginning of the target or subject string regardless of the /m
flag.
Upvotes: 4
Reputation: 342433
If you read perldoc perlretut, this is an extract that is useful to your understanding.
· s modifier (//s): Treat string as a single long line. '.' matches any character, even "\n". "^" matches only at the beginning of the string
and "$" matches only at the end or before a newline at the end.
· m modifier (//m): Treat string as a set of multiple lines. '.' matches any character except "\n". "^" and "$" are able to match at the start
or end of any line within the string.
· both s and m modifiers (//sm): Treat string as a single long line, but detect multiple lines. '.' matches any character, even "\n". "^" and
"$", however, are able to match at the start or end of any line within the string.
Here are examples of "//s" and "//m" in action:
$x = "There once was a girl\nWho programmed in Perl\n";
$x =~ /^Who/; # doesn't match, "Who" not at start of string
$x =~ /^Who/s; # doesn't match, "Who" not at start of string
$x =~ /^Who/m; # matches, "Who" at start of second line
$x =~ /^Who/sm; # matches, "Who" at start of second line
$x =~ /girl.Who/; # doesn't match, "." doesn't match "\n"
$x =~ /girl.Who/s; # matches, "." matches "\n"
$x =~ /girl.Who/m; # doesn't match, "." doesn't match "\n"
$x =~ /girl.Who/sm; # matches, "." matches "\n"
Most of the time, the default behavior is what is wanted, but "//s" and "//m" are occasionally very useful. If "//m" is being used, the start of
the string can still be matched with "\A" and the end of the string can still be matched with the anchors "\Z" (matches both the end and the
newline before, like "$"), and "\z" (matches only the end):
$x =~ /^Who/m; # matches, "Who" at start of second line
$x =~ /\AWho/m; # doesn't match, "Who" is not at start of string
$x =~ /girl$/m; # matches, "girl" at end of first line
$x =~ /girl\Z/m; # doesn't match, "girl" is not at end of string
$x =~ /Perl\Z/m; # matches, "Perl" is at newline before end
$x =~ /Perl\z/m; # doesn't match, "Perl" is not at end of string
Upvotes: 0
Reputation: 9269
Yes. \A
will match at the very beginning of your value. ^
will match the beginning of the value, but will also match immediately after newlines in multiline mode (//m
).
The \Z
is similar, but with the end of the value. However, it will also match before a newline at the end of the value. If you don't want this behaviour, use \z
, which matches only at the end of the value.
Useful reference: perlre manpage
Upvotes: 10
Reputation: 6078
Yes, according to the documentation:
The \A, \Z, and \z assertions differ from the traditional circumflex and dollar (described in the next section) in that they only ever match at the very start and end of the subject string, whatever options are set.
Upvotes: 2