Reputation: 1441
for example:
string str = $@"<html> < head > </ head > < body > <start> < h1 > Header 1 </ h1 > < p > A worker can be of 3 different types.</ p > <end> < p ></ p > </ body > </ html > "
string replacement = "hello world";
string newString = $@"<html> < head > </ head > < body > <start> < h1 > Header 1 </ h1 > < p > hello world</ p > <end> < p ></ p > </ body > </ html > "
So I have a <start>
and <end>
sign to know which part of the text should be replaced. How I can get the newString
by regex.
Upvotes: 1
Views: 6550
Reputation: 37299
Using Regex.Replace
you set the pattern from the first <r>
to the second, including all that is in between. Then you specify what to replace with.
var result = Regex.Replace(str, "<start>.*?<end>", $"<start> {replacement} <end>");
If prior to C# 6.0 string interpolation then:
var result = Regex.Replace(str, "<start>.*?<end>", string.Format("<start> {0} <end>",replacement));
With latest string from comments:
string str = $@"<html> < head > </ head > < body > <start> < h1 > Header 1 </ h1 > < p > A worker can be of 3 different types.</ p > <end> < p ></ p > </ body > </ html > ";
string replacement = "hello world";
var result = Regex.Replace(str, "<start>.*?<end>", $"<start> {replacement} <end>");
Upvotes: 4