mwhite14
mwhite14

Reputation: 257

Removing part of a string C#

I am using a rich text editor on my website and i am using a label to display what the user entered last time he used the box, but when he submits the code it includes the html from the label. When I submit the form it writes the following....

<span id="bodyContent_header1"> <div style"float:right">The Text On The Page </div> </span>

I need to remove the <span id="bodyContent_header1"> </span> from the code and keep the rest. Can anyone help? Also the code might include other span tags, so it should only remove this one each time.

Upvotes: 2

Views: 372

Answers (2)

RePierre
RePierre

Reputation: 9566

Given that such strings are a single line you can use the regex ^<span[^>]+>|</span>$ to replace the outermost <span> tags like this:

string strRegex = @"^<span[^>]+>|</span>$";
RegexOptions myRegexOptions = RegexOptions.Multiline | RegexOptions.Singleline;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"<span id=""bodyContent_header1""> <div style""float:right"">The Text On The Page </div> </span>\n";
string strReplace = @"";
return myRegex.Replace(strTargetString, strReplace);

This works on RegexHero.

Upvotes: 3

burning_LEGION
burning_LEGION

Reputation: 13450

this regex select data from this tag

(?<=<span id="bodyContent_header1">)(.+)(</span>)

resolve this task with one regex replace imposible

Upvotes: 1

Related Questions