in reply to Re: RegEx - match pattern not followed by literal
in thread RegEx - match pattern not followed by literal

No, that doesn't work.
$users_regexp = qr/^./s; 'abc,v' = /$users_regex(?!,v$)/; # Matches, but it shouldn't. $users_regexp = qr/^.*$/s; 'abc,v' = /$users_regex(?!,v$)/; # Matches, but it shouldn't. $users_regexp = qr/,/; 'abc,v' = /$users_regex(?!,v$)/; # Matches, but it shouldn't.

Solution:

/^(?=.*?$users_regex)(?!.*?,v$)/s
which simplifies to
/^(?!.*,v$).*?$users_regex/s

Of course, this is not nearly as readable as the original.

Updated to show more examples where the parent doesn't work.

Replies are listed 'Best First'.
Re^3: RegEx - match pattern not followed by literal
by Melly (Chaplain) on Oct 12, 2006 at 15:53 UTC

    Heh - yeah, looks like the single-regex solution is just too darned ugly (and I'd feel guilty handing that on to be maintained).

    I'll stick with my original two regex solution (but implement that bastard Fletch's suggestion to swap the regexes and check for ,v$ first)

    I guess I was just suprised that the reverse, /$users_regex,v$/, was so easy, but handling NOT ,v$ is so tricky...

    Tom Melly, tom@tomandlu.co.uk

      Regexps can do 'followed by' and 'or' easily, but not 'and'.

      I just thought of another solution:

      /$users_regex .*\z (?<!,v) (?<!,v\n)/xs

      Or if you really meant /,v\z/:

      /$users_regex .*\z (?<!,v)/xs
        You guys are making this a little more complicated than it needs to be. I think that /(?!.*,v$)$users_regex/ will suffice. As has already been said, though, the OP's first solution is probably cleanest.