我需要一种使用RegEx搜索文本的方法,并在Latex命令中找到一个单词(这意味着它在花括号中)
下面是一个例子:
Tarzan is my name and everyone knows that {Tarzan loves Jane}现在,如果您搜索regex:({[^{}]*?)(Tarzan)([^}]*})并将其替换为$1T~a~r~z~a~n$3
这将只取代词泰山内的花括号和忽略另一个实例!这就是我来的地方。
现在,我需要的是对以下示例进行同样的操作:
Tarzan is my name and everyone knows that {Tarzan loves Jane} but she doesn't know that because its written with \grk{Tarzan loves Jane}在这个例子中,我只需要最后一次提到“泰山”就可以被替换(\grk{}中的那个)。
有人能帮我修改上面的RegEx搜索吗?
发布于 2016-02-05 16:29:48
您可以尝试使用以下模式:
(?:\G(?!\A)|\\grk{)[^}]*?\KTarzan演示
详情:
(?:
\G(?!\A) # contiguous to a previous match
| # OR
\\grk{ # first match
)
[^}]*? # all that is not a } (non-greedy) until ...
\K # reset the start of the match at this position
Tarzan # ... the target word注意:\G匹配上一次匹配后的位置,但它也匹配字符串的开始。这是因为我添加了(?!\A),以防止字符串开头出现匹配。
或者您可以使用:多个pass的\\grk{[^}]*?\KTarzan。
https://stackoverflow.com/questions/35228719
复制相似问题