Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between \z and \Z and \a and \A in Perl

Tags:

regex

perl

Can you please tell me the difference between \z and \Z as well as \a and \A in Perl with a simple example ?

like image 222
user3597719 Avatar asked Sep 11 '15 15:09

user3597719


People also ask

What is the difference between * and in regex?

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 ...

What is the use \w in Perl?

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).

What does \d mean in Perl?

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.

What does \s mean in Perl?

Substitution Operator or 's' operator in Perl is used to substitute a text of the string with some pattern specified by the user.


Video Answer


2 Answers

\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.

like image 191
ruakh Avatar answered Oct 13 '22 19:10

ruakh


  • \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.
    • It is equivalent to \x07 on an ASCII-based machine.
    • It is equivalent to \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.

like image 21
ikegami Avatar answered Oct 13 '22 17:10

ikegami