in reply to /s does not match end-of-line

Hello glendeni,

Yes, /\s/ does match a newline character:

13:41 >perl -wE "say qq[Matched >$1<] if qq[abc2345] =~ /([0-9]+)\s/; 13:41 >perl -wE "say qq[Matched >$1<] if qq[abc2345 ] =~ /([0-9]+)\s/; Matched >2345< 13:42 >perl -wE "say qq[Matched >$1<] if qq[abc2345\n] =~ /([0-9]+)\s/ +; Matched >2345< 13:42 >perl -v This is perl 5, version 26, subversion 0 (v5.26.0) built for MSWin32-x +64-multi-thread-ld

For the meaning of \s in a regex, see “Whitespace” in perlrecharclass#Backslash-sequences.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

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

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