leslie has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

what is the difference between \z and \Z. can any one explain with example ?

For example: Difference between /\babc\z/ and /\babc\Z/.

  • Comment on zero-width assertions in perl regular expression

Replies are listed 'Best First'.
Re: zero-width assertions in perl regular expression
by GrandFather (Saint) on Feb 28, 2009 at 10:26 UTC

    perlre can:

    \Z Match only at end of string, or before newline at the end
    \z Match only at end of string

    Update Consider:

    my $str = "The quick abc"; for my $regex ('\babc\z', '\babc\Z') { print "Matched with newline using $regex\n" if "$str\n" =~ $regex; print "Matched without newline using $regex\n" if $str =~ $regex; }

    Prints:

    Matched without newline using \babc\z Matched with newline using \babc\Z Matched without newline using \babc\Z

    True laziness is hard work

      Thanks for your replay.
      Its clarifies me.

Re: zero-width assertions in perl regular expression
by spadacciniweb (Curate) on Feb 28, 2009 at 10:44 UTC
    Simple script to explain the difference:
    use strict; use warnings; use YAPE::Regex::Explain; for my $regexp ('\babc\z','\babc\Z') { print YAPE::Regex::Explain->new($regexp)->explain, "\n"x3; }
    print:
    The regular expression: (?-imsx:\babc\z) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- \b the boundary between a word char (\w) and something that is not a word char ---------------------------------------------------------------------- abc 'abc' ---------------------------------------------------------------------- \z the end of the string ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- The regular expression: (?-imsx:\babc\Z) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- \b the boundary between a word char (\w) and something that is not a word char ---------------------------------------------------------------------- abc 'abc' ---------------------------------------------------------------------- \Z before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    (($_="Mzz ojjdloobnf jt uvy5502383")=~y~b-zg2-5c96-81~a-z0-9~s)=~s~~~s; print