Can you please tell me the difference between \\z
and \\Z
as well as \\a
and \\A
in Perl with a simple example ?
\z
only matches the very end of the string.
\Z
also matches the very end of the string, but if the string ends with a newline, then \Z
also matches immediately before the newline.
So, for example, these five are true:
'foo' =~ m/foo\z/
'foo' =~ m/foo\Z/
"foo\n" =~ m/foo\Z/
"foo\n" =~ m/foo\n\z/
"foo\n" =~ m/foo\n\Z/
whereas this one is false:
"foo\n" =~ m/foo\z/
They both differ from $
in that they are not affected by the /m
"multiline" flag, which allows $
to match at the end of any line.
\a
denotes the alert (bell) character; it doesn't have any additional special meaning in a regex.
\A
matches only at the start of a string. Like \z
and \Z
, and unlike ^
, it's not affected by the /m
"multiline" flag.
All of this is documented in perlre
, the Perl regular expressions manual page: http://perldoc.perl.org/perlre.html.
The following indicates the positions at which the relevant regex patterns will match (␊
indicates a line feed):
\A \A is not affected by /m
^ ^ without /m ≡ \A
^/m ^/m ^/m ^ with /m ≡ \A|(?<=\n)
| | |
| | |
v v v
abc␊def␊ghi␊
^ ^ ^^
| | ||___
| | | |
$/m $/m $/m $/m $ with /m ≡ (?=\n)|\z
$ $ $ without /m ≡ (?=\n\z)|\z
\Z \Z \Z is not affected by /m ≡ (?=\n\z)|\z
\z \z is not affected by /m
\a
is equivalent to \x07
, meaning it matches character 0x07 (BEL/BELL in ASCII and UNICODE).
This is documented in perlre.