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