0

I am using regular expressions to replace any formatting within span tags using the following expression and it works.

retValue = System.Text.RegularExpressions.Regex.Replace(retValue, @"<span[^>]*>", "<span>");

Now, I would like to replace any formatting within the span tags except for 'Underline'. For example in the following string, I would like to remove the formatting within the second span tag, but keep the formatting of the first span tag.

 string retValue = "<p><span style=\"text-decoration: underline;\">Test Underline</span></P><p><span style="color:blue">blue</span></p>";

So my retValue should be :

retValue = "<p><span style=\"text-decoration: underline;\">Test Underline</span></P><p><span>blue</span></p>";

I tried using the following expression, but it doesn't replace anything at all. I am trying to understand what is wrong with this code and how can I achieve the expected result.

retValue = System.Text.RegularExpressions.Regex.Replace(retValue, @"<span[^style=\""text-decoration:underline;>]*>", "<span>");
Sri
  • 31
  • 2
  • 8
  • Possible duplicate of [Regex: match everything but](https://stackoverflow.com/questions/1687620/regex-match-everything-but) – 41686d6564 stands w. Palestine Dec 03 '18 at 01:24
  • 1
    Can you make any firm and solid guarantees that the content of the style attribute would not look different than shown in your question? There could be any number of variations of the style attribute, with different CSS properties (of which `text-decoration` would only be one of them) in arbitrary order. And then `text-decoration` itself could not only have a single `underline` value, but a combination value like `text-decoration: red underline`, for example. Unless you can guarantee that the style attribute will never have a varying content, well: https://stackoverflow.com/a/1732454/2819245 –  Dec 03 '18 at 01:45
  • 1
    You might want to consider using a html parser to deal with this instead of regular expressions. – juharr Dec 03 '18 at 01:50

1 Answers1

-2

You need to escape the special character correctly:

var pattern = "\\<span[^style\\=\\\"text\\-decoration\\:underline\\;\\>]*>";
retValue = System.Text.RegularExpressions.Regex.Replace(retValue, pattern, "<span>");
Xiaosu
  • 605
  • 1
  • 9
  • 21
  • 1
    This is not answering the question, and it is not a meaningful regex pattern at all. Please look up a tutorial about regex, specifically about the function of `[` `]` and `^` within `[` `]`. –  Dec 03 '18 at 02:28