I have some HTML content that I need to modify using C#. It is conceptually simple but I'm not sure how to do it efficiently. The content contains several occurrences of delimited numbers followed by an empty anchor tag. I need to take the delimited number and insert it into a JavaScript function call in the anchor tag. E.G.
The source string would contain something like this:
%%1%%<a href="#"></a>
<p>A bunch of HTML markup</p>
%%2%%<a href="#"></a>
<p>Some more HTML markup</p>
I need to transform it to this:
<a href="#" onclick="DoSomething('1')></a>
<p>A bunch of HTML markup</p>
<a href="#" onclick="DoSomething('2')></a>
<p>Some more HTML markup</p>
There is no limit to the number of %%\d+%% occurrences. I took a crack at writing a regular expression in hopes I could use the Replace method, but I'm not sure if that can even work with multiple instances of each group. Here's what I had:
%%(?<LinkID>\d+)%%(?<LinkStart><a[\s\S]*?)(?:(?<LinkEnd>>[\s\S]*?)(?=%%\d+|$))
// %%(?<LinkID>\d+)%% Match a number surrounded by %% and put the number in a group named LinkID
// (?<LinkStart><a[\s\S]*?) Match <a followed by any characters until next match (non greedy), in a group named LinkStart
// (?: Logical grouping that does not get captured
// (?<LinkEnd>>[\s\S]*?) Match > followed by any characters until next match, in a group named LinkEnd
// (?=%%\d+%%|$) Where the former LinkEnd group is followed by another instance of a delimited number or the end of the string. (I don't think this is working as I intended.)
Maybe some combination of a couple Regex operations and String.Format could be used. I'm not an expert at regular expressions.