in reply to Re^3: \s does not match end-of-line
in thread /s does not match end-of-line

Since I'm matching chomped single lines, not multiple lines, is there something which "\Z" can do which "$" does not?? (Did not know about "\z" anchor - that may have been put in after I learned perl in 1996.)

Replies are listed 'Best First'.
Re^5: \s does not match end-of-line
by AnomalousMonk (Archbishop) on Feb 06, 2018 at 06:08 UTC

    My personal regex best practices (solidly in line with TheDamian's as given in his PBP book) include reserving  ^ $ for matching around embedded newlines (which means that the  /m regex modifier is always asserted) and using  \A \z \Z as "absolute" string start/end anchors. To the best of my recollection,  \A \z \Z were introduced with Perl 5, sometime prior to 1996 (update: but see hippo's correction).

    ... is there something which "\Z" can do which "$" does not??

    It's the other way around:  ^ $ match also around embedded newlines (with the ever-present  /m modifier), while  \A \z \Z match only at string start/end; hence, in a sense, they do less. The advantage of this practice is that you never have to think about what  ^ $ do; also,  \A \z \Z cannot be modified.


    Give a man a fish:  <%-{-{-{-<

      To the best of my recollection, \A \z \Z were introduced with Perl 5, sometime prior to 1996.

      Not quite. \z at least was introduced with perl 5.005 in 1998. I believe that the others do predate this, however.