开发者

What regular expression would strip out all attributes from a BR tag?

开发者 https://www.devze.com 2022-12-23 14:14 出处:网络
What C# regular expression would 开发者_如何学Pythonreplace all of these: <BR style=color:#93c47d>

What C# regular expression would 开发者_如何学Pythonreplace 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".


Something like:

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

Or without the IgnoreCase:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消