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

As Athanasius's examples here demonstrate, the  \s class requires a whitespace character (if  * ? quantifiers are not present), and that's not what you have if you chomp off the newline. You may be interested to investigate the  $ \z \Z line end anchors. In your particular case,  \Z (big-Z) might be useful to "Match only at end of string, or before newline at the end" (see perlre); a  \Z would match match the end of either a chomp-ed or un-chomp-ed line (assuming you're chomp-ing newlines).

Update: Fixed  ^ vice  $ in "... the  ^ \z \Z line end anchors." per Lotus1's /msg. Thanks!


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

Replies are listed 'Best First'.
Re^4: \s does not match end-of-line
by glendeni (Sexton) on Feb 06, 2018 at 05:14 UTC
    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.)

      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.