Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Regex bug? matching multiple newline with /m

by tybalt89 (Monsignor)
on Aug 09, 2021 at 08:31 UTC ( [id://11135723]=note: print w/replies, xml ) Need Help??


in reply to Regex bug? matching multiple newline with /m

$ is a zero-width match, it does not consume a \n;

[rick@ry ~]$ perl -e 'use strict; printf "%s\n", "ab\nab\n" =~ /^ab$ab +$/m' Global symbol "$ab" requires explicit package name (did you forget to +declare "my $ab"?) at -e line 1. Execution of -e aborted due to compilation errors. [rick@ry ~]$

Try adding the 'use strict;' to your other examples to see what they are really doing.

Replies are listed 'Best First'.
Re^2: Regex bug? matching multiple newline with /m
by NERDVANA (Deacon) on Aug 09, 2021 at 10:41 UTC
    Ha, yes bitten by interpolation. Thanks.

    Now I'm wondering how many times in the past I've had difficulties with a multiline regex and it was due to the incorrect assumption that it would consume the \n...

      ... bitten by interpolation.

      Perhaps better to say "bitten by failure to enable strict" (and I would also recommend warnings).


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

      The problem with interpolation is covered in the documentation for  /PATTERN/msixpodualngc in Regexp Quote Like Operators.
      If "'" is used as the delimiter, no variable interpolation is done.

      This is not enough to solve your problem with the newline because $ is a zero width assertion. Assertions

      Besides "^" and "$", Perl defines the following zero-width assertions:

      In this case, it is probably best to match the newline explicitly and not use the anchor at all.

      Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11135723]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-16 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found