EDIT: This example uses html, but I need this type of scenario for working with other types of strings. Please read this as a regex issue, not a html issue.
Let's say I have a string like this:
<h1>Hello</h1><h2>World</h2><h3>!</h3>
I may need to replace text to any one of those heading tags, but let's use this example, where I just want to modify <h2>
to look like this:
<h1>Hello</h1><div id="h2div"></div><h2>World</h2><h3>!</h3>
Since I may need to replace any of the headings, I only search for <h*
using regex. Now, I want my code to say "of all the <h*
tags you found, only replace the second one".
I thought I found the answer here: How do I replace a specific occurrence of a string in a string?
Unfortunately, the results are not what I am looking for. Here is my sample code:
private void button1_Click(object sender, EventArgs e)
{
//sample html file string:
var htmlText = "<h1>Hello</h1><h2>World</h2><h3>!</h3>";
//this text should replace <h2 with <div id="h2div"></div><h2"
var replacementString = "<div id=\"" + "h2div" + "\"" + "</div>" + "<h2";
int replacementIndex = 1; //only replace the second occurence found by regex.
//find ALL occurrences of <h1 through <h6 in the file, but only replace <h2.
htmlText = Regex.Replace(htmlText, "<h([1-6])", m => replacementString + replacementIndex++);
}
It does not matter whether I specify replacementIndex
or replacementIndex++
, which makes sense but I just wanted to match the code as closely as possible to the answer I found.
The output looks like this:
<div id="h2div"></div><h21>Hello</h1><div id="h2div"></div><h22>World</h2><div id="h2div"></div><h23>!</h3>
There are lots of things that should not be happening here. First, only one <div>
tag should have been created, rather than three. Second, the <h
tag is only replaced instead of <h2
, so now we end up with <h21
, <h22
, and <h23
.
From a few months ago, I'm getting better at understanding regex matching but I am really unfamiliar with regex matchevaluators and groups; which I guess is what I probably need here.
Could you recommend how I can fix the code so I can replace a specific index of a regex match?