Can you please tell me the difference between \z
and \Z
as well as \a
and \A
in Perl with a simple example ?
represents any single character (usually excluding the newline character), while * is a quantifier meaning zero or more of the preceding regex atom (character or group). ? is a quantifier meaning zero or one instances of the preceding atom, or (in regex variants that support it) a modifier that sets the quantifier ...
A \w matches a single alphanumeric character (an alphabetic character, or a decimal digit) or _ , not a whole word. Use \w+ to match a string of Perl-identifier characters (which isn't the same as matching an English word).
The Special Character Classes in Perl are as follows: Digit \d[0-9]: The \d is used to match any digit character and its equivalent to [0-9]. In the regex /\d/ will match a single digit. The \d is standardized to “digit”. The main advantage is that the user can easily write in shorter form and can easily read it.
Substitution Operator or 's' operator in Perl is used to substitute a text of the string with some pattern specified by the user.
\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.
\A
matches zero characters at position 0.\z
matches zero characters at the end of the string.\Z
matches zero characters at the end of the string and at a trailing line feed.^
without /m
is the same as \A
.^
with /m
matches zero characters at position 0 and after a line feed.$
without /m
is the same as \Z
.$
with /m
matches zero characters at the end of the string and at a line feed.\a
matches the BEL/BELL character.
\x07
on an ASCII-based machine.\x2F
on an EBCDIC-based machine.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)
| | |
0123456789012
| | |
v v v
abc␊def␊ghi␊
^ ^ ^^
| | ||
0123456789012
| | ||___
| | | |
$/m $/m $/m $/m $ with /m ≡ \z|(?=\n)
$ $ $ without /m ≡ \z|(?=\n\z)
\Z \Z \Z is not affected by /m ≡ \z|(?=\n\z)
\z \z is not affected by /m
This is documented in perlre.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With