in reply to Re: Using lookaround with variables
in thread Using lookaround with variables

Actually, if $pre, $searchTerm and $post are text rather than regexp patterns, ...

Actually, if $pre contains anything that would involve a variable-length match, it just won't work with the look-behind operator:

perl -e '$pre="x+"; $_="xxFindme"; print "found" if(/(?<=$pre)Findme/) +' # dies with the message: Variable length lookbehind not implemented in regex; marked by <-- HER +E in m/(?<=x+)Findme <-- HERE / at -e line 1.
That is, the regex quantifiers "*", "?", "+" and "{n,m}" cannot be used as such when doing a look-behind (though they will work with look-ahead). If the OP's $pre might ever contain one of these things, putting \Q...\E around it in the regex -- treating them as literals -- will at least make sure the script doesn't crash. -- update: and as ikegami explains below, there's a better way to handle this

(I'm sure ikegami knows this already, but it might not have been clear to mwunderlich or other readers.)

Replies are listed 'Best First'.
Re^3: Using lookaround with variables
by ikegami (Patriarch) on Dec 17, 2008 at 07:19 UTC

    While you're right that very few patterns work in a lookbehind, it's wrong to add \Q..\E based on the possibility that the variable might contain patterns that aren't allowed in lookbehinds. Whether \Q..\E should be used or not is strictly based on whether $pre contains a pattern or literal text. Your advice would break the pattern /a.b/, for example.

    If you want to catch invalid patterns, you need eval BLOCK, not \Q..\E.