in reply to How do you make a regex non greedy

$data =~ s/.*?(?=TEST)//;

Replies are listed 'Best First'.
Re^2: How do you make a regex non greedy
by GrandFather (Saint) on Mar 20, 2011 at 04:56 UTC

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

    s/^.*?(?=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
      Anchoring isn't needed here either
      $_ = '1 TEST 2 TEST 3 TEST'; s/.*?(?=TEST)//; print; __END__ TEST 2 TEST 3 TEST