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

Thanks for confirming my understanding. Looking for something else to explain my result, realized I had fixated on regexp but actual problem was input of the lines from a file followed by chomp to strip the end-of-lines.

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

    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:  <%-{-{-{-<

      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:  <%-{-{-{-<