Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Tell regex to stop at "phrase", instead of char - how? (greed)

by tye (Sage)
on Aug 02, 2007 at 21:45 UTC ( [id://630406]=note: print w/replies, xml ) Need Help??


in reply to Re: Tell regex to stop at "phrase", instead of char - how?
in thread Tell regex to stop at "phrase", instead of char - how?

That's fine so long as the regex ends at that point. Backtracking can easily cause problems if that pattern is part of some larger regex (or later becomes part of some larger regex).

For example, if I want to find only sections of "start...stop" that actually end in "stop now" then non-greedy doesn't try hard enough to not be greedy:

$_= "start this stop start that stop now start last stop"; print "non-greedy:\n"; print " ", $_, $/ for /start(.*?)stop now/g; print "look-ahead:\n"; print " ", $_, $/ for /start((?:(?!stop).)*)stop now/g; __END__ non-greedy: this stop start that look-ahead: that

- tye        

  • Comment on Re^2: Tell regex to stop at "phrase", instead of char - how? (greed)
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found