昨天,我尝试替换文本中的所有 (
)
,但不替换在先前搜索的匹配项中找到的内容。我找不到好的解决方案。我是一一改的。
这是我的原文:
(Peter Jackson) is the man who..... (John Johnson) is the man who....
and (John Johnson) is also the man who.. not as (Jon Johnson)
more (letters) etc.
我的搜索模式:/(\a*\sJohnson)
如何将文本中的所有 (
)
字符替换为 [
]
,而不是匹配中找到的字符?
结果应该是:
[Peter Jackson] is the man who..... (John Johnson) is the man who....
and (John Johnson) is also the man who.. not as (Jon Johnson)
more [letters] etc.
编辑
是否有一种简单的方法可以替换文本中的一个或多个任意字符,但不能替换匹配中的这些字符?
最佳答案
您可以使用\@!
( Negative Lookahead ):
:%s/(\%(\a*\sJohnson\)\@!\([^)]*\))/[\1]/g
->|............|<---
\%(...\)\@!
是负向预测
。\([^)]*\)
将匹配除)
之外的所有内容。- 我使用
\%(
来忽略捕获的组,否则您需要/[\2]/
关于search - 替换某些字符,但不替换匹配项中找到的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10061055/