Search code examples
regex

Match RegEx except where preceded by character


I want to match a certain RegEx ABC except where an occurrence is preceded by a backtick.

So from this I want the first ABC but not the second.

Hello ABC `ABC

Using RegEx with negative lookahead:

(?!`)ABC

Both occurrences are matched:

enter image description here

Note, I don't want the backtick as part of the match so not this:

[^`]ABC

I would also like to match ABC when it is not preceded by any character at all i.e.

ABC `ABC

Should also match the first ABC


Solution

  • Try: (?<!`)ABC

    As mentioned by anubhava, you should use negative look behind "(?<!`)ABC"