正则表达式/grep 匹配:"not this" 和 "that"

Regex / grep match on: "not this" and "that"

从 Linux 命令行,我想在多个文件中找到所有实例,其中我没有使用 Fig. 引用图形引用。

所以我正在寻找每一行,当我没有在 \ref{fig 前加上 Fig.

  1. Fig. \ref{fig:myFigure}
  2. A sentence with Fig. \ref{fig:myFigure} there.
  3. \ref{fig:myFigure}
  4. A sentence with \ref{fig:myFigure} there.

正则表达式应忽略情况 (1) 和 (2),但会找到情况 (3) 和 (4)。

您可以像这样使用 Negative Lookahead:

^((?!Fig\. {0,1}\ref\{fig).)*$

https://regex101.com/r/wSw9iI/2

Negative Lookahead (?!Fig\.\s*\ref\{fig)
Assert that the Regex below does not match
Fig matches the characters Fig literally (case sensitive)
\. matches the character . literally (case sensitive)
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\ matches the character \ literally (case sensitive)
ref matches the characters ref literally (case sensitive)
\{ matches the character { literally (case sensitive)
fig matches the characters fig literally (case sensitive)