Reputation: 94
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. Each span may have none, one or multiple occurrences of the string {abc}
inside, e.g.
<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.
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.
What I have is,
find: (<span class="xyz">)([^<]*)\{abc\}([^<]*<)
replace: \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.
Upvotes: 1
Views: 680
Reputation: 29677
This seems to work in Notepad++
Find what : (?:<span class="xyz">|\G)[^<]*?\K\{abc\}(?=[^<]*<\/span>)
Replace with : {def}
Search mode : Regular expression
Note that because of the [^<]*
there is an assumption that there are no other tags within the span tag.
Upvotes: 1