in reply to reg ex NOT

[^t] matches any character but t. It can be modified using ?, + and *, as usual.

You might also be interested in (?:(?!regexp).)*, which matches a sequence of characters that does not contain anything that matches a specified regexp.

Replies are listed 'Best First'.
Re^2: reg ex NOT
by brian_d_foy (Abbot) on Jun 16, 2006 at 00:18 UTC

    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

      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.)