in reply to Re: reg ex NOT
in thread reg ex NOT

Remember that using * means to match zero or more times and that ? matches zero or one times, and that you can always match zero times. When you match zero times, you don't prevent anything from not matching:

print "Matched" if "dont" =~ m/don(?:(?!t).)*/;

You could anchor it so that nothing after it could match:

print "Matched" if "dont" =~ m/don(?:(?!t).)*$/;
It's easier just to type the negated character class for that position, though:
print "Matched" if "dont" =~ m/don[^t]/;
--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

Replies are listed 'Best First'.
Re^3: reg ex NOT
by ikegami (Patriarch) on Jun 16, 2006 at 01:33 UTC

    Aye, the construct I posted needs to be anchored (but not necessarily using ^ and $). It may be a waste to use that construct for single characters, but it's useful (necessary) for the use I described in my original post (i.e. To matche a sequence of characters that does not contain anything that matches a specified regexp). For example,

    /<table>(?:(?!<\/table>).)*<\/table>/

    (Not the best example, cause tables can be nested in HTML and for other reasons, but you get the idea.)