in reply to Re: How do you make a regex non greedy
in thread How do you make a regex non greedy

Not so simple. You have also to anchor the left hand end:

s/^.*?TEST/TEST/

Update: Argh - one day I'll stop getting bitten by that! Anonymous Monk is quite right. No backtracking is required to get a match so .*? matches everything from the start or the string up to TEST - no anchor needed.

True laziness is hard work

Replies are listed 'Best First'.
Re^3: How do you make a regex non greedy
by Anonymous Monk on Mar 20, 2011 at 05:03 UTC
    Yes, it is precisely that simple
    $_ = '1 TEST 2 TEST 3 TEST'; s/.*?TEST/TEST/; print; __END__ TEST 2 TEST 3 TEST