正则表达式只有在字符不存在时才匹配。

huangapple go评论51阅读模式
英文:

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

我正在寻找一个正则表达式,只有当字符串中不存在单词KO或OK时才匹配(无论前后有什么)。

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
英文:

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

答案1

得分: 0

我认为你在寻找类似这样的内容:(?!.*(OK|KO).*)^.+$,这里的^.+$是主要匹配(它匹配整行),而(!?....)部分则进行否定匹配,因此不会匹配包含OK|KO的文本。

英文:

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

huangapple
  • 本文由 发表于 2023年3月21日 00:18:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定