[英]Find and replace all occurrences of a string inside an HTML tag in one pass
I need a regular expression to search for and replace multiple occurrences of a text string within a delimited section of text. 我需要一个正则表达式来搜索和替换文本的定界部分内多次出现的文本字符串。
Let's say there is HTML code with one or more spans that have a certain class. 假设存在具有某个类的一个或多个span的HTML代码。 Each span may have none, one or multiple occurrences of the string
{abc}
inside, eg 每个跨度在其中可能不包含,不出现一次或多次出现字符串
{abc}
,例如
<p>lorem ipsum dolor <span class="xyz">sid amet{abc}et pluribus {abc} unum{abc} diex
et mon droit</span> you'll never walk alone</p>
Thus I need a regex pair to replace all occurrences of {abc}
within <span id="xyz">
with {def}
in a single pass . 因此,我需要一个正则表达式对,以单遍将
{def}
<span id="xyz">
所有出现的{abc}
替换为{def}
。
This is for use in a text editor such as Notepad++ and the like and needs to be be a PCRE/UNIX-style regular expression. 这用于文本编辑器(例如Notepad ++等)中,并且必须是PCRE / UNIX样式的正则表达式。
What I have is, 我所拥有的是
find: (<span class="xyz">)([^<]*)\\{abc\\}([^<]*<)
查找:
(<span class="xyz">)([^<]*)\\{abc\\}([^<]*<)
replace: \\1\\2{def}\\3
替换:
\\1\\2{def}\\3
This does work for one occurrence within a span, but in case of more occurrences, I have to run replacement multiple times, in cycle, while I need that to be one-pass . 这确实适用于跨度中的一次出现,但是如果出现更多次,我必须循环运行多次替换,而我需要一次通过 。
I wonder how can I achieve that. 我不知道该如何实现。 I suppose this is a pretty common case, somehow I could not find similar things concerning the need to be one-pass, no cycles, no code, and I'd like to get an idea how this could be done in principle.
我想这是一个很普通的情况,以某种方式,我找不到与需要一次性通过,没有周期,没有代码的类似事情,并且我想知道如何在原则上做到这一点。
This seems to work in Notepad++ 这似乎在记事本++中工作
Find what : (?:<span class="xyz">|\\G)[^<]*?\\K\\{abc\\}(?=[^<]*<\\/span>)
查找什么:
(?:<span class="xyz">|\\G)[^<]*?\\K\\{abc\\}(?=[^<]*<\\/span>)
Replace with : {def}
替换为:
{def}
Search mode : Regular expression 搜索方式:正则表达式
Note that because of the [^<]*
there is an assumption that there are no other tags within the span tag. 请注意,由于
[^<]*
因此假设span标签内没有其他标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.