What regular expression will highlight all attributes from the BR tag?

Which C # regex will replace all of them:

<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 type it in lowercase."

+3
source share
2 answers

Sort of:

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

Or without IgnoreCase:

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

Assuming you never had any attributes after the style, I would put 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 software solution if there are attributes written to the tag after the style attribute.

0
source

Source: https://habr.com/ru/post/1738045/


All Articles