可以使用負先行,如:
^((?!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)
怎麼樣'Fig.'和'\ ref'之間換行符? –
@BenjaminW。 - 是的,這是一種可能性。我將它從原文中省略,專注於「not this」和「that」所需的正則表達式。可選的linebreak不會難以添加。 –
可選的換行符有很大的不同,因爲grep逐行查看文件。如果你告訴它使用另一個字符而不是換行符,比如空字符,則匹配將返回整個文件,因爲它被認爲是單行。 –