替换所有匹配项,但被包围或仅当被包围时除外 [英] Replacing all matches except if surrounded by or only if surrounded by
本文介绍了替换所有匹配项,但被包围或仅当被包围时除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定文本字符串(减价文档),我需要实现以下两个选项之一:
替换整个文档中特定表达式(
(W)(theWord)(W)
)的所有匹配项除标记图像语法![Blah theWord blah](url)
内的匹配项。替换特定表达式(
{{([^}}]+)}}[[[^]]]+]]
)仅的所有匹配项,即:![Blah {{theWord}}[[1234]] blah](url)
。
这两个表达式当前都匹配所有内容,无论是否在标记图像语法中,而且我已经尝试了我能想到的所有内容。
Here is an example of the first option
And here is an example of the second option
我们非常感谢您提供的任何帮助和/或线索。
提前感谢!
推荐答案
嗯,我稍微修改了一下第一个表达式,因为我认为有一些额外的捕获组,然后通过添加提前查找技巧来创建它们:
-第一个(Live demo):
(vitae)(?![^[]*]s*()
-第二个(Live demo):
{{([^}}]+)}}[[[^]]]+]](?=[^[]*]s*()
前视部件说明:
(?! # Starting a negative lookahead
[^[]*] # Everything that's between brackets
s* # Any whitespace
( # Check if it's followed by an opening parentheses
) # End of lookahead which confirms the whole expression doesn't match between brackets
(?=
表示积极向前看
这篇关于替换所有匹配项,但被包围或仅当被包围时除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文