195

Can regular expression be utilized to match any string except a specific string constant (i.e. "ABC")?

Is it possible to exclude just one specific string constant?

3
  • 1
    Which tool are you using? Depending upon the tool, there might be a way to specify this external to your regex. grep supports a -v option to invert the sense of the match, for example. Commented Sep 8, 2009 at 17:23
  • So are you looking to match every character of a given string, except the ABC part of it? In other words, "A string with ABC" would match "A string with ". Commented Sep 8, 2009 at 19:06
  • This is not a duplicated question, the other post is of a specific case that focus on a line. \ This is for a more general case focus on "any content". \ For now I have no good answer to this, but you may use silly things like, \ eg: (?<!(?=f(?=or)|(?<=f)o(?=r)|(?<=fo)r)). \ An apple for AA, an orange for BB or DD, CC is forbidden to have anything. \ to match any single char but exclude (' char inside) a string sequence for. Commented Aug 13, 2024 at 23:19

5 Answers 5

211

You have to use a negative lookahead assertion.

(?!^ABC$)

You could for example use the following.

(?!^ABC$)(^.*$)

If this does not work in your editor, try this. It is tested to work in ruby and javascript:

^((?!ABC).)*$
Sign up to request clarification or add additional context in comments.

8 Comments

This will work if you're looking for a string that does not include ABC. But is that the goal? Or is the goal to match every character except ABC?
Thanks for pointing that out, you are right - my suggestion only avoids strings starting with ABC - I forgot to anchor the assertion. Going to correct that.
That's still different than what I was thinking. Perhaps the questioner will clarify what they're looking for.
I find it quite clear - "any string except a specific string [constant]" hence any string (including strings containing ABC) except ABC itself.
I was helping a friend recently to do something very similar. However, he didn't want to match the string if it contained a string anywhere inside of it. So I wrote a slightly modified version of your expression (?!.*ABC)^.*$ and this works like a charm.
|
10

In .NET you can use grouping to your advantage like this:

http://regexhero.net/tester/?id=65b32601-2326-4ece-912b-6dcefd883f31

You'll notice that:

(ABC)|(.)

Will grab everything except ABC in the 2nd group. Parenthesis surround each group. So (ABC) is group 1 and (.) is group 2.

So you just grab the 2nd group like this in a replace:

$2

Or in .NET look at the Groups collection inside the Regex class for a little more control.

You should be able to do something similar in most other regex implementations as well.

UPDATE: I found a much faster way to do this here: http://regexhero.net/tester/?id=997ce4a2-878c-41f2-9d28-34e0c5080e03

It still uses grouping (I can't find a way that doesn't use grouping). But this method is over 10X faster than the first.

Comments

9

This isn't easy, unless your regexp engine has special support for it. The easiest way would be to use a negative-match option, for example:

$var !~ /^foo$/
    or die "too much foo";

If not, you have to do something evil:

$var =~ /^(($)|([^f].*)|(f[^o].*)|(fo[^o].*)|(foo.+))$/
    or die "too much foo";

That one basically says "if it starts with non-f, the rest can be anything; if it starts with f, non-o, the rest can be anything; otherwise, if it starts fo, the next character had better not be another o".

3 Comments

That won’t allow the empty string, f, fo and foo.
@Gumbo: It allows the empty string just fine; notice that ($) is the first alternative, so ^$ (empty string) is accepted. I tested it, and at least in perl 5.0.10 the empty string is accepted.
... sorry, perl 5.10.0, of course!
8

Try this regular expression:

^(.{0,2}|([^A]..|A[^B].|AB[^C])|.{4,})$

It describes three cases:

  1. less than three arbitrary character
  2. exactly three characters, while either
    • the first is not A, or
    • the first is A but the second is not B, or
    • the first is A, the second B but the third is not C
  3. more than three arbitrary characters

Comments

7

You could use negative lookahead, or something like this:

^([^A]|A([^B]|B([^C]|$)|$)|$).*$

Maybe it could be simplified a bit.

Comments