RegEx to exclude a specific string constant

V Govind picture V Govind · Sep 8, 2009 · Viewed 329.7k times · Source

Can regular expression be utilized to match any string except a specific string constant let us say "ABC" ? Is this possible to exclude just one specific string constant? Thanks your help in advance.

Answer

Daniel Brückner picture Daniel Brückner · Sep 8, 2009

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