Ras
Ras

Reputation: 628

C# RegExp to remove span tag and keep quotes char

I have to remove the span tags inside a string such:

<span>Operation Gambling:</span><span>la mano della crimitalità </span><span>sull'azzardo</span>

To do this, I use the following regexp:

Regex.Replace(inHTML, "<span[^>]*?>", string.Empty).Replace("</span>", "&nbsp;</span>");

the result sometimes is correct but in this case is:

Operazione Gambling: la mano della crimitalità sull&nbsp;azzardo

As you can see the single quote has been remove, how can I keep it by modifying the pattern?

Upvotes: 1

Views: 2833

Answers (1)

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

You can use this code for removing HTML tag inside your string:

var str = "<span>Operation Gambling:</span><span>la mano della crimitalità </span><span>sull'azzardo</span>";
String result = Regex.Replace(str, @"<[^>]*>", String.Empty);
System.Console.WriteLine(result);

Or this regex for removing just span tags:

Regex.Replace(str, @"</?span( [^>]*|/)?>", String.Empty);

Upvotes: 5

Related Questions