Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Why isn't this regex greedy?

by kettle (Beadle)
on Mar 17, 2006 at 10:33 UTC ( [id://537424]=note: print w/replies, xml ) Need Help??


in reply to Why isn't this regex greedy?

I may be totally off my rocker here but I think that perl will interpret:

$_ = 'fooxbarbar'; # Not greedy - why not? if(/(foo)((?!bar).){1,5}bar/){ print "1 Matched:$1 $2\n"; } else{ print "1 Not Matched\n"; }


exactly the same way it interprets your code from expression #1:

$_ = 'fooxbarbar'; # Not greedy - why not? if(/(foo)(((?!bar).){1,5})bar/){ print "1 Matched:$1 $2\n"; } else{ print "1 Not Matched\n"; }


(note the missing set of parentheses on the top, modified version of your expression)

The output of both is, in any case, the same. The salient point being that perl is not including the
{1,5}
in the backreference - thus making the expression non-greedy...?

There is a great little blurb on positive and negative lookahead that you might take a look at:

http://www.regular-expressions.info/lookaround.html

wonder if that makes sense....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found