Hanuman
Hanuman

Reputation: 97

Exclude <br> tag from regex not to remove from text?

I have code like this

string pattern = "<(.|\n)+?>";
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Reg(pattern);
string result = "";
result = regEx.Replace(htmlText, "");

In this "htmlText" will have some html code which also contains break tags. Right now its replacing all the html tags, but I want to leave break tag and replace the rest. How can i do it? Anybody have any idea?

Thanks

Upvotes: 3

Views: 3022

Answers (2)

Martin Buberl
Martin Buberl

Reputation: 47144

This should work:

string html = "<span>test<br><br /></span>";
Regex regex = new Regex("<[^(?!br)>]*>", RegexOptions.Compiled);
string result = regex.Replace(html, string.Empty);

Upvotes: 0

Linora
Linora

Reputation: 10988

You could try this one:

<(?!br|/br).+?>

Upvotes: 3

Related Questions