in reply to regex problem

Another solution involves a negative lookahead assertion (see perlre):

if ( /^if(?!f)/ ) { # begins with if (but not with iff) } else { # anything else }
The (?!f) part imposes a zero-width condition on the match: it must not be followed by the character 'f'. The difference between this solution and /^if[^f]/ is that the latter regex fails on the string 'if'. In other words, with the negative lookahead, the match has length 2, whereas with a (negative) character class the match has length 3.

the lowliest monk

Replies are listed 'Best First'.
Re^2: regex problem
by monarch (Priest) on Jun 08, 2005 at 12:54 UTC
    The above solution by tlm is clearly superior to that I'm about to suggest here.

    But another approach that I've used in environments (like Emacs) that don't have negative lookaheads is:

    /if([^f]|$)/
    or
    /if(?:[^f]|$)/
    to ensure nothing is returned in $1 should the regex match.