Search code examples
regexregex-negation

Regex is match just if a character is not present


I am looking for a regex that finds the match only and only if either the word KO or the word OK is not present within the string (regardless of what is before and after).

I tried this:

[^OK|KO]

But it doesn't work.

example of what I would like to match:

  •                   20032023
    

example of what I would like NOT to match:

  • 20032023 KO
  • OK 123456 20032023

Solution

  • I think you are looking for something like this (?!.*(OK|KO).*)^.+$. here ^.+$ is a main match (it matches whole lines), and (!?....) piece makes the negation, so matches that also contain OK|KO won't be taken