As an introductory note, I am aware of the old saying about solving problems with regex and I am also aware about the precautions on processing XML with RegEx. But please bear with me for a moment...
I am trying to do a RegEx search and replace on a group of characters. I don't know in advance how often this group will be matched, but I want to search with a certain context only.
An example:
If I have the following string "**ab**df**ab**sdf**ab**fdsa**ab**bb"
and I want to search for "ab"
and replace with "@ab@"
, this works fine using the following regex:
Search regex:
(.*?)(ab)(.*?)
Replace:
$1@$2@$3
I get four matches in total, as expected. Within each match, the group IDs are the same, so the back-references ($1, $2 ...) work fine, too.
However, if I now add a certain context to the string, the regex above fails:
Search string:
<context>abdfabsdfabfdsaabbb</context>
Search regex:
<context>(.*?)(ab)(.*?)</context>
This will find only the first match.
But even if I add a non-capturing group to the original regex, it doesn't work ("<context>(?:(.*?)(ab)(.*?))*</context>"
).
What I would like is a list of matches as in the first search (without the context), whereby within each match the group IDs are the same.
Any idea how this could be achieved?