in reply to stupid question about regexp - not matching a word

I'm not exactly sure what you're trying to do... are you trying to match only strings that do not contain "abc"? If so, you may want something like this:
print unless /\babc\b/;
Are you trying to match "nevermind" only if not preceded by "abc"? If so, you may want something like this, which uses negative lookbehind:
print if /(?<!abc )nevermind/;
Are you trying to match every word except "abc"? If so, you may want something like this:
my @words = grep !/^abc\z/, split; print "@words";

-- Mike

--
just,my${.02}

Replies are listed 'Best First'.
Re: Re: stupid question about regexp - not matching a word
by december (Pilgrim) on Jul 15, 2002 at 22:21 UTC

    Actually, I was trying to match every sentence where 'abc' is not preceding 'nevermind' - every other word is ok.

    Negative look-behind works, but I expected a more common combination to work too - something like grouping letters with () to a word, and then ^ to negate the sense. Is there no way to group these letters as a word instead of individual characters?

    It's more a matter of curiosity if it can be done with just a simple regexp rather than making it work, otherwise I'd easily solve it with more code.

    Thanks! ;)

      Do you not want to use lookbehind because of efficiency concerns, or some other reason? It seems to be just the right tool for the job... It's doing what you were trying to do in your original code: making sure "abc" does not match before "nevermind".

      As far as using [^abc] to do the job, [] represents a character class within regular expressions, and can only match a single character at a time. So unfortunately, you can't use [] to match anything more than a single character.

      -- Mike

      --
      just,my${.02}

        It's not really because of efficiency concerns, I would like my regexp (if possible) to work with the sadly enough more simple c regexp function in sed and grep. I'm not sure that look-behinds are supported.

        I'm just learning more about regexp's, which is why I wanted to solve it without multiple lines of perl code. I'm a bit surprised that you can group several words in an or-chain, but not negate the matching of a single word without look-behind.

        Not really a problem though, I'll just use look-behinds. Thanks again for your time...

           wouter

      The Cookbook has the following solution for your problem:
      /^(?:(?!abc).)*$/
      This matches a string that does not contain the text abc.

      Abigail