tags:

views:

131

answers:

2

What C# regular expression would replace all of these:

<BR style=color:#93c47d>
<BR style=color:#fefefe>
<BR style="color:#93c47d">
<BR style="color:#93c47d ...">
<BR>
<BR/>
<br style=color:#93c47d>
<br style=color:#fefefe>
<br style="color:#93c47d">
<br style="color:#93c47d ...">
<br>
<br/>

with:

<br/>

basically "remove all attributes from any BR element and lowercase it".

+6  A: 

Something like:

Regex.Replace(myString, "<br[^>]*>", "<br/>", RegexOptions.IgnoreCase);

Or without the IgnoreCase:

Regex.Replace(myString, "<[Bb][Rr][^>]*>", "<br/>");
Michael Myers
that works nicely, thanks!
Edward Tanguay
A: 

Assuming you never had any attributes after style, I would bet something like

class Program
{
  const string SOURCE = @"<BR style=color:#93c47d>
<BR style=color:#fefefe>
<BR style=""color:#93c47d"">
<BR style='color:#93c47d'>
<BR>
<BR/>
<br style=color:#93c47d>
<br style=color:#fefefe>
<br style=""color:#93c47d"">
<br style='color:#93c47d'>
<br>
<br/>";

  static void Main(string[] args)
  {
    const string EXPRESSION = @"(style=[^""'][^>]*)|(style=""[^""]*"")|(style='[^']*')";

    var regex = new Regex(EXPRESSION);

    Console.WriteLine(regex.Replace(SOURCE, string.Empty));
  }
}

You might be better off with a programmatic solution if there are attributes written into a tag after the style attribute.