Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

/s does not match end-of-line

by glendeni (Acolyte)
on Feb 06, 2018 at 03:04 UTC ( #1208520=perlquestion: print w/replies, xml ) Need Help??
glendeni has asked for the wisdom of the Perl Monks concerning the following question:

I seem to be confused as to what a whitespace "\s" matches in a perl regexp. I had thought it should match a new line (ie "\n"), so was trying to use "(0-9+)\s" to match a number followed by EITHER a blank (which in turn can be followed by other characters, eg a comment) OR an end of line. But this fails to match the latter case.

Replies are listed 'Best First'.
Re: \s does not match end-of-line
by Athanasius (Bishop) on Feb 06, 2018 at 03:37 UTC

    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,

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

Re: /s does not match end-of-line
by Anonymous Monk on Feb 06, 2018 at 14:27 UTC
    I also observe that in your original post you wrote /s ... a forward slash. Notice that certain modifier flags can appear after the regex itself, which of course is bounded by forward slashes ... /foo\s*bar/sx. These characters are not part of the pattern itself.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1208520]
Approved by Athanasius
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-12-13 06:41 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (62 votes). Check out past polls.