我怎样才能匹配所有没有用所需的 LaTeX 标记标记的短语?
How can I match all occurrences of a phrase not tagged with desired LaTeX tag?
目的
查找所有未用 \senator{
}
标记的 "Bernie Sanders"。我想列出与 grep
的匹配项以供目视检查。然后我想用一个命令递归地修复所有文件(例如 sed
,它不支持非贪婪正则表达式)。
示例文件
Bernie Sanders
\senator{Bernie Sanders}
The senator of Vermont is \senator{Bernie Sanders}.
A \texttt{senator of Vermont} is Bernie Sanders.
A senator of Vermont is \textit{Bernie Sanders}.
\textit{Bernie Sanders} is a senator of Vermont.
Is this the same Bernie Sanders?
Is Bernie Sanders a good senator?
Will we ever see a \textbf{President Bernie Sanders}?
问题
正则表达式不能"accidentally"干扰其他命令
一次尝试:
\[^senator]*{Bernie Sanders
我不确定如何排除参议员,但包括 spaces,以及 Bernie Sanders 之前的其他命令。
开始
- 可以通过 space
- \senator{
未进行
- 可能是一行中的第一件事
结束
- 可能以 space、冒号、分号、问号、逗号、圆括号、括号(如果在另一个命令中)结束,完整 stop/period
仅当前缀 \senator 不存在时才会匹配
(?<!\senator\{)Bernie\sSanders
(?<!\senator{)Bernie Sanders(?!\s*})
您可以将它与 grep -P
一起使用。Lookarounds
将确保它不包含在 senator
tag.See 演示中。
grep
的解决方案(基本假设:\senator{Bernie Sanders} 出现在一行中;同一行中没有好的和坏的命中)
grep "Bernie Sanders" input.tex | grep -v -e '\senator{Bernie Sanders}'
另一个解决方案是使用 perl 脚本。以下单行代码正在处理您的示例(它使用前面的正则表达式之一):
perl -pe 's/(?<!\senator{)Bernie\sSanders/The New Bernie Sanders/g' input.tex > output.tex
基本上它用字符串 "The New Bernie Sanders" 替换文件 input.tex
中正则表达式的任何匹配项。结果打印在output.tex
.
此外,此脚本可以包含在 bash 脚本中以处理多个文件:
#!/bin/bash
for i in {1..3}
do
perl -pe 's/(?<!\senator{)Bernie\sSanders/The New Bernie Sanders/g' input$i.tex > output$i.tex
done
此脚本处理文件 input1.tex
、input2.tex
、input3.tex
并打印文件 output1.tex
、output2.tex
、output2.tex
。
(循环是非常基本的,但我的意思只是表明单行代码可以很容易地包含在 bash 脚本中)。
目的
查找所有未用 \senator{
}
标记的 "Bernie Sanders"。我想列出与 grep
的匹配项以供目视检查。然后我想用一个命令递归地修复所有文件(例如 sed
,它不支持非贪婪正则表达式)。
示例文件
Bernie Sanders
\senator{Bernie Sanders}
The senator of Vermont is \senator{Bernie Sanders}.
A \texttt{senator of Vermont} is Bernie Sanders.
A senator of Vermont is \textit{Bernie Sanders}.
\textit{Bernie Sanders} is a senator of Vermont.
Is this the same Bernie Sanders?
Is Bernie Sanders a good senator?
Will we ever see a \textbf{President Bernie Sanders}?
问题
正则表达式不能"accidentally"干扰其他命令
一次尝试:
\[^senator]*{Bernie Sanders
我不确定如何排除参议员,但包括 spaces,以及 Bernie Sanders 之前的其他命令。
开始
- 可以通过 space
- \senator{ 未进行
- 可能是一行中的第一件事
结束
- 可能以 space、冒号、分号、问号、逗号、圆括号、括号(如果在另一个命令中)结束,完整 stop/period
仅当前缀 \senator 不存在时才会匹配
(?<!\senator\{)Bernie\sSanders
(?<!\senator{)Bernie Sanders(?!\s*})
您可以将它与 grep -P
一起使用。Lookarounds
将确保它不包含在 senator
tag.See 演示中。
grep
的解决方案(基本假设:\senator{Bernie Sanders} 出现在一行中;同一行中没有好的和坏的命中)
grep "Bernie Sanders" input.tex | grep -v -e '\senator{Bernie Sanders}'
另一个解决方案是使用 perl 脚本。以下单行代码正在处理您的示例(它使用前面的正则表达式之一):
perl -pe 's/(?<!\senator{)Bernie\sSanders/The New Bernie Sanders/g' input.tex > output.tex
基本上它用字符串 "The New Bernie Sanders" 替换文件 input.tex
中正则表达式的任何匹配项。结果打印在output.tex
.
此外,此脚本可以包含在 bash 脚本中以处理多个文件:
#!/bin/bash
for i in {1..3}
do
perl -pe 's/(?<!\senator{)Bernie\sSanders/The New Bernie Sanders/g' input$i.tex > output$i.tex
done
此脚本处理文件 input1.tex
、input2.tex
、input3.tex
并打印文件 output1.tex
、output2.tex
、output2.tex
。
(循环是非常基本的,但我的意思只是表明单行代码可以很容易地包含在 bash 脚本中)。