正则表达式/ grep匹配:“不是这个”和“那个”

时间:2017-09-13 02:57:03

标签: regex grep regex-negation

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

所以当我没有准确\ref{figFig. 作序言时,我正在寻找每一行。

  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.
  5. 正则表达式应该忽略案例(1)和(2),但是找到案例(3)和(4)。

1 个答案:

答案 0 :(得分:1)

您可以使用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)