I have HTML in a string that looks like this:
<div id="control">
<a href="/xx/x">y</a>
<ul>
<li><a href="/C003Q/x" class="dw">x</a></li>
<li><a href="/C003R/xx" class="dw">xx</a></li>
<li><a href="/C003S/xxx" class="dw">xxx</a></li>
</ul>
</div>
I would like to change this to the following:
<div id="control">
<a data-href="/xx/x" ><span>y</span></a>
<ul>
<li><a data-href="/C003Q/x" class="dw"><span>x</span></a></li>
<li><a data-href="/C003R/xx" class="dw"><span>xx</span></a></li>
<li><a data-href="/C003S/xxx" class="dw"><span>xxx</span></a></li>
</ul>
</div>
I heard about regex but I am not sure how I can use it to change something inside the address tags and to change href at the same time. Would I need to use regex twice and can I change the inside of the <a ... >...</a> using regex or is there an easier way with C#?
You can use a regular expression replace. Use parentheses to catch values in the text that you match, and use
$1,$2et.c. to use the values in the replacement string:Note: If the HTML code doesn’t have that exact same form, the replace won’t work. If there for example is another attribute in the anchor tag, or if the attribue order is reversed, the pattern won’t match.