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

Maybe I'm missing something subtle here, but the answers so far seem too complicated.

If you want to stop at the phrase 'stop', just embed that at the appropriate point in the regex and use an non-greedy 'anything' match:

$s = "this is a load of junk to be consumed until we get to the word s +top. After that nothing should stop it until another stop";; print $1 while $s =~ m[(.+?)stop]g;; this is a load of junk to be consumed until we get to the word . After that nothing should it until another

And if you want the stop phrase to be a part of (say) the next capture, only then do you need to wrap it in a ZLA:

print $1 while $s =~ m[(.+?)(?=stop)]g;; this is a load of junk to be consumed until we get to the word stop. After that nothing should stop it until another

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Tell regex to stop at "phrase", instead of char - how? (greed)
by tye (Sage) on Aug 02, 2007 at 21:45 UTC

    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