user2085236
user2085236

Reputation: 49

How to do a Regex Replace for this text?

How to do a regex replace for this text:

    <span style=\"text-decoration: underline;\">Request Block Host</span> to

    `<u>Request Block Host</u>`

So far, I have this, assume "text" is the complete string that has the above tag.

   text = Regex.Replace(text, "<span style=\"text-decoration: underline;\">.*?</span>", delegate(Match mContent)
        {
            return mContent.Value.Replace("<span style=\"text-decoration: underline;\">", "<u>").Replace("</span>", "</u>");
        }, RegexOptions.IgnoreCase);      

Upvotes: 0

Views: 541

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149040

This should do the trick:

text = Regex.Replace(text, 
    "<span style=\"text-decoration: underline;\">(.*?)</span>", 
    "<u>$1</u>",
    RegexOptions.IgnoreCase); // <u>Request Block Host</u>

This will match a literal <span style="text-decoration: underline;">, followed by zero or more of any character, captured in group 1, followed by a literal </span>. It will replace the matched text with <u>, followed by the contents of group 1, followed by a literal </u>.

Upvotes: 3

user817530
user817530

Reputation:

var _string = "<span style=\"text-decoration: underline;\">Request Block Host</span>";

var text = Regex.Replace(_string, "<.+>(.*)</.+>", "<u>$1</u>");

:D

Upvotes: 1

Related Questions