我在 tex 文件中有一些变量应该括在 中,\texttt{}
但实际上却没有。它们通常是用下划线分隔的两个单词:word1_word2
。我可以使用带有 的正则表达式找到这些变量[\w]*\\_[\w]*
。但是,我只想找到这些变量未被 括起来的实例\texttt{}
,并将其替换为\texttt{[\w]*\\_[\w]}
。
我花了一个小时左右的时间试图弄清楚如何做到这一点,但还是没能成功。例如,^(\\texttt\{)[\w]*\_[\w]*)
不起作用。
我正在使用 Libre Office Writer。
这是另一个例子:
- 寻找:
?<!(\{.*)\b([:alnum:])+\\_([:alnum:])+\b(?!\})
- 代替:
\\texttt{$0}
如您所见,它显示未找到搜索关键字,而在最后三个中,\items
有new\_ID
、new\_code
和new\_storage
的实例未包含在 中\texttt{}
,而在每个相同的 中,\item
还有 中每个实例的另一个实例\texttt{}
,以及其他文本。我希望它用 、 等替换这些实例。\texttt{new\_ID}
请注意每行末尾的变化:
前:
\item \texttt{Change\_ID(ID, new\_ID):} Changes the ID of the object with the given ID to new\_ID.
\item \texttt{Change\_code(ID, new\_code):} Changes the code of the object with the given ID to new\_code.
\item \texttt{Change\_storage(ID, new\_storage):} Changes the storage of the object with the given ID to new\_storage.
后:
\item \texttt{Change\_ID(ID, new\_ID):} Changes the ID of the object with the given ID to \texttt{new\_ID}.
\item \texttt{Change\_code(ID, new\_code):} Changes the code of the object with the given ID to \texttt{new\_code}.
\item \texttt{Change\_storage(ID, new\_storage):} Changes the storage of the object with the given ID to \texttt{new\_storage}.
答案1
我知道发生了什么,正则表达式必须被固定下来。
这是一个可行的解决方案,但它每行仅替换 1 个出现的情况。如果有些行出现超过 1 次,则必须按Replace all多次。
- 寻找:
^(.*?)((?<!\{)\b\w+\\_\w+\b(?!\}))
- 代替:
$1\\texttt{$2}
解释:
^ # begining of line
(.*?) # group 1, 0 or more any character but newline, not greedy
( # start group 2
(?<!\{) # negative lookbehind, make sure we haven't "{" before
\b # word boundary
\w+ # 1 or more word characters
\\_ # an escaped underscore
\w+ # 1 or more word characters
\b # word boundary
(?!\}) # negative lookahead, make sure we haven't "}" after
) # end group 2
屏幕截图:
答案2
对于 LibreOffice Writer 以及 notepadqq 或 sublimeText 等编辑器来说,此类工作所需的正则表达式过于复杂。
它可以与 Notepad++ 一起使用,但是由于您是 Linux 用户,我建议您使用以下 Perl 单行命令:
perl -ape 's/^.*?\{.*?\}(*SKIP)(*FAIL)|(.*?)((?<!\{)\b\w+\\_\w+\b(?!\}))/$1\\texttt{$2}/g' file
perl -i.bak -ape ...
如果你想替换文件,可以使用,原始文件将以扩展名备份.back
输出:
\item \texttt{Change\_ID(ID, new\_ID):} Changes the ID of the object with the given ID to \texttt{new\_ID}.
\item \texttt{Change\_code(ID, new\_code):} Changes the code of the object with the given ID to \texttt{new\_code}.
\item \texttt{Change\_storage(ID, new\_storage):} Changes the storage of the object with the given ID to \texttt{new\_storage}.
解释:
s/ # substitute
^ # begining of line
.*?\{.*?\} # 0 or more any character followed by 0 or more character between curly braces
(*SKIP)(*FAIL) # if found, forget it
| # OR
(.*?) # group 1, 0 or more any character but newline, not greedy
( # start group 2
(?<!\{) # negative lookbehind, make sure we haven't "{" before
\b # word boundary
\w+ # 1 or more word characters
\\_ # an escaped underscore
\w+ # 1 or more word characters
\b # word boundary
(?!\}) # negative lookahead, make sure we haven't "}" after
) # end group 2
/ # delimiter, replace with:
$1 # content of group 1
\\texttt{$2} # suround group 2
/g # global replacement