1

我需要一种方法来使用 RegEx 搜索文本并在 Latex 命令中找到一个单词(这意味着它在花括号内)

这是示例:

Tarzan is my name and everyone knows that {Tarzan loves Jane}

现在,如果您搜索 regex:({[^{}]*?)(Tarzan)([^}]*})并将其替换为$1T~a~r~z~a~n$3

这将仅替换花括号内的 Tarzan 一词并忽略另一个实例!这是我来的为止。

现在我需要对以下示例做同样的事情:

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}

在这个例子中,我只需要替换最后提到的“Tarzan”(\grk{} 中的那个)

有人可以帮我修改上面的 RegEx 搜索来做到这一点吗?

4

1 回答 1

2

您可以尝试使用这种模式:

(?:\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)以防止字符串开头的匹配。

或者您可以使用:\\grk{[^}]*?\KTarzan多次通过。

于 2016-02-05T16:29:48.607 回答