假设我有一个像这样的字符串
<other...Stuff> BoundsTag <relevant...Stuff> EndsBoundsTag <other...Stuff> BoundsTag <relevant...Stuff> EndsBoundsTag <other...Stuff>
我想在我的字符串上进行搜索和替换,但只有在BoundsTag / EndsBoundsTag中才能更改它。我正在尝试匹配的字符串在<relevant...Stuff>
和<other...Stuff>
中都存在很多次。此外,还有任意数量的BoundsTag / EndsBoundsTag对。
Perl正则表达式可以实现吗?
以下是我尝试替换MyMatch
BoundsTag asdfasdfa MyMatch asdfasdfasdf MyMatch sdfasd EndsBoundsTag asdfasdfasdfsad **MyMatch** asd *MyMatch** asf2ef23fasdfasdf BoundsTag fghjfghj MyMatch fghjfghjgh MyMatch fghjfghj EndsBoundsTag
在这里,我想要替换除**
之间的所有MyMatch实例。我并不是指具体的**
字符,只是指出它们。此外,间距只是为了易读。
答案 0 :(得分:0)
假设这些标签总是成对出现并且无法使用,那很简单:
/Stuff(?=(?:(?!BoundsTag).)*EndsBoundsTag)/s
仅当Stuff
可以匹配EndsBoundsTag
后,才匹配BoundsTag
,中间没有Stuff # Match Stuff
(?= # only if the following matches afterwards:
(?: # 1. A group that matches...
(?!BoundsTag) # ...unless it's the start of "BoundsTag"...
. # any character,
)* # repeated as needed.
EndsBoundsTag # 2. EndsBoundsTag must also be present
) # End of lookahead - if that succeeds, we're between tags.
。
在regex101.com上测试它。
<强>解释强>
{{1}}
答案 1 :(得分:0)