如果该行上存在特定单词,如何使用正则表达式取消匹配该行?

How to use regex to unmatch the line if certain word exists on that line?

我有以下正则表达式

(?!.*internal).*auditor['’s]*.*(?=report)|(?!.*internal)(?<=report).*auditor['’s]*.*

和以下测试用例

report of auditor
report of external auditor
auditor external report
in auditor report
auditor report
internal report of auditor
report of internal auditor
auditor internal report

如果 auditor['’s]* 之前或之后有 report 我想匹配,但如果单词 internal 出现

我不想匹配

与我上面的正则表达式 internal report of auditor 将匹配。

这是想要的结果

report of auditor
report of external auditor
auditor external report
in auditor report
auditor report

这是regex101

"auditor""'s" 后缀似乎无关紧要,因此删除不必要的并发症。

您的要求可以表示为:

  • 包含"auditor"
  • 包含 "report"(因为“某物之前或之后”仅表示“包含”——与“某物”无关)
  • 不包含"internal"

将其放入正则表达式中:

^(?!.*\binternal\b)(?=.*\breport\b).*\bauditor\b.*

我在术语周围放置了单词边界 (\b),因此例如“内化”和“报告”不匹配。

请参阅 live demo,显示此匹配除了示例输入的最后 3 行之外的所有行。