Using C# regular expressions to remove HTML tags

216,535

Solution 1

As often stated before, you should not use regular expressions to process XML or HTML documents. They do not perform very well with HTML and XML documents, because there is no way to express nested structures in a general way.

You could use the following.

String result = Regex.Replace(htmlDocument, @"<[^>]*>", String.Empty);

This will work for most cases, but there will be cases (for example CDATA containing angle brackets) where this will not work as expected.

Solution 2

The correct answer is don't do that, use the HTML Agility Pack.

Edited to add:

To shamelessly steal from the comment below by jesse, and to avoid being accused of inadequately answering the question after all this time, here's a simple, reliable snippet using the HTML Agility Pack that works with even most imperfectly formed, capricious bits of HTML:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(Properties.Resources.HtmlContents);
var text = doc.DocumentNode.SelectNodes("//body//text()").Select(node => node.InnerText);
StringBuilder output = new StringBuilder();
foreach (string line in text)
{
   output.AppendLine(line);
}
string textOnly = HttpUtility.HtmlDecode(output.ToString());

There are very few defensible cases for using a regular expression for parsing HTML, as HTML can't be parsed correctly without a context-awareness that's very painful to provide even in a nontraditional regex engine. You can get part way there with a RegEx, but you'll need to do manual verifications.

Html Agility Pack can provide you a robust solution that will reduce the need to manually fix up the aberrations that can result from naively treating HTML as a context-free grammar.

A regular expression may get you mostly what you want most of the time, but it will fail on very common cases. If you can find a better/faster parser than HTML Agility Pack, go for it, but please don't subject the world to more broken HTML hackery.

Solution 3

The question is too broad to be answered definitively. Are you talking about removing all tags from a real-world HTML document, like a web page? If so, you would have to:

  • remove the <!DOCTYPE declaration or <?xml prolog if they exist
  • remove all SGML comments
  • remove the entire HEAD element
  • remove all SCRIPT and STYLE elements
  • do Grabthar-knows-what with FORM and TABLE elements
  • remove the remaining tags
  • remove the <![CDATA[ and ]]> sequences from CDATA sections but leave their contents alone

That's just off the top of my head--I'm sure there's more. Once you've done all that, you'll end up with words, sentences and paragraphs run together in some places, and big chunks of useless whitespace in others.

But, assuming you're working with just a fragment and you can get away with simply removing all tags, here's the regex I would use:

@"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)>"

Matching single- and double-quoted strings in their own alternatives is sufficient to deal with the problem of angle brackets in attribute values. I don't see any need to explicitly match the attribute names and other stuff inside the tag, like the regex in Ryan's answer does; the first alternative handles all of that.

In case you're wondering about those (?>...) constructs, they're atomic groups. They make the regex a little more efficient, but more importantly, they prevent runaway backtracking, which is something you should always watch out for when you mix alternation and nested quantifiers as I've done. I don't really think that would be a problem here, but I know if I don't mention it, someone else will. ;-)

This regex isn't perfect, of course, but it's probably as good as you'll ever need.

Solution 4

Regex regex = new Regex(@"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>", RegexOptions.Singleline);

Source

Solution 5

@JasonTrue is correct, that stripping HTML tags should not be done via regular expressions.

It's quite simple to strip HTML tags using HtmlAgilityPack:

public string StripTags(string input) {
    var doc = new HtmlDocument();
    doc.LoadHtml(input ?? "");
    return doc.DocumentNode.InnerText;
}
Share:
216,535
tuutuutuut
Author by

tuutuutuut

Just wanted the badge ;)

Updated on June 02, 2021

Comments

  • tuutuutuut
    tuutuutuut almost 3 years

    How do I use C# regular expression to replace/remove all HTML tags, including the angle brackets? Can someone please help me with the code?

    • Rafael
      Rafael over 11 years
    • John
      John over 10 years
      You don't indicate it, but I'm inferring that you also want to remove script and style elements entirely and not just remove the tag. The HTML Agility Pack answer below is correct for removing the tags, but to remove script and style, you'll also need something like stackoverflow.com/questions/13441470/…
    • goodeye
      goodeye about 10 years
      The question indicated as a duplicate has a lot of information (and Tony the Pony!), but it only asked for opening tags, not all tags. So I'm not sure it's technically a duplicate. That said, the answer is the same: don't.
  • Ryan Emerle
    Ryan Emerle about 15 years
    This is a naive implementation.. That is, <div id="x<4>"> is unfortunately, valid html. Handles most sane cases though..
  • Daniel Brückner
    Daniel Brückner about 15 years
    As stated, I am aware that this expression will fail in some cases. I am not even sure if the general case can be handled by any regular expression without errors.
  • Jake
    Jake about 15 years
    No this will fail in all cases! its greedy.
  • Alan Moore
    Alan Moore about 15 years
    @Cipher, why do you think greediness is a problem? Assuming the match starts at the beginning of a valid HTML tag, it will never extend beyond the end of that tag. That's what the [^>] is for.
  • PropellerHead
    PropellerHead over 14 years
    HTML Agility Pack is not the answer to everything related to working with HTML (e.g. what if you only want to work with fragments of the HTML code?!).
  • JasonTrue
    JasonTrue over 14 years
    It works pretty well with fragments of HTML, and it's the best option for the scenario described by the original poster. A Regex, on the other hand, only work with an idealized HTML and will break with perfectly valid HTML, because the grammar of HTML is not regular. If he were using Ruby, I still would have suggested nokogiri or hpricot, or beautifulsoup for Python. It's best to treat HTML like HTML, not some arbitrary text stream with no grammar.
  • JasonTrue
    JasonTrue about 13 years
    HTML is not a regular grammar, and therefore cannot be parsed solely with regular expressions. You can use regexes for lexing, but not for parsing. It's really that simple. Linguists would have agreed on this before HTML even existed.
  • JasonTrue
    JasonTrue about 13 years
    This isn't a matter of opinion. A regular expression may get you mostly what you want most of the time, but it will fail on very common cases. If you can find a better/faster parser than HTML Agility Pack, go for it, but please don't subject the world to more broken HTML hackery.
  • Desolator
    Desolator over 12 years
    Regex can work with basic and simple parsing. however, you still can extract all of the links from any HTML as the links or URLs have a fixed pattern...
  • Dylan Vester
    Dylan Vester over 12 years
    I don't understand what you guys are arguing over? The OP wants to replace/remove HTML tags, and mentioned nothing of parsing it. HTML Agility Pack is overkill.
  • JasonTrue
    JasonTrue over 12 years
    You can't correctly identify HTML tags reliably without parsing HTML. Do you understand all of the grammar for HTML? See the evil hack to get "pretty close" that other answers suggest, and tell me why you'd want to have to maintain that. Downvoting me because a hacky quick attempt works for your sample input isn't going to make your solution correct. I've occasionally used regexes the generate reports from HTML content or to fix up some CSS reference using negative matching on &gt; to limit the chance of errors, but the we did additional verifications; it wasn't general purpose.
  • JWilliams
    JWilliams over 12 years
    This is by far the best answer. You answer the poster's question and explain why a regular expression should not be used for the given task. Well done.
  • Kache
    Kache about 12 years
    @AlanMoore html is not a "regular language", i.e. you can't properly match everything that is valid html with regexes. see: stackoverflow.com/questions/590747/…
  • jessehouwing
    jessehouwing about 12 years
    The following code would do: HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(Properties.Resources.HtmlContents); var text = doc.DocumentNode.SelectNodes("//body//text()").Select(node => node.InnerText); StringBuilder output = new StringBuilder(); foreach (string line in text) { output.AppendLine(line); } string textOnly = HttpUtility.HtmlDecode(output.ToString());
  • Steve Pettifer
    Steve Pettifer about 11 years
    Whilst I'm a bit late on this I'd like to mention that this also works on xml such as that produced by Word and other office products. anyone who's ever had the need to deal with Word xml would do well to look at using this because it does help a lot, especially if you need to strip tags from content which is exactly what I needed it for.
  • ChrisF
    ChrisF about 11 years
    @Jake, no it's delimited so greedy is better. If you made that ungreedy, it'd be way slower.
  • ChrisF
    ChrisF about 11 years
    Apart from obvious crossplatform linebreak issues, having an ungreedy quantifier is slow when the content is delimited. Use things like <xml>.*(?!</xml>)</xml> with the RegexOptions.SingleLine modifier for the first two and <[^>]*> for the last. The first ones can also be combined by a captured alternation in the first tag name and backreferences to it in the negative lookahead and final tag.
  • Jason
    Jason almost 9 years
    This is a helpful answer because I just need to rip out a.hrefs from a line of text for an email subject line.
  • ahwm
    ahwm almost 9 years
    For my solution I don't need to be parsing out or processing an HTML document. All I need is to strip out HTML that will be made incomplete after truncating to X number of characters (and therefore screwing up the look of the web page).
  • GRUNGER
    GRUNGER over 8 years
    why not <[^>].+?> ?
  • giparekh
    giparekh over 6 years
    It removes the hyperlink set on text as well. How can I stop removing hyperlink set on simple text? like <a href="http..">test link</a>
  • Ted Krapf
    Ted Krapf about 4 years
    When all else seemed to fail, this simple code snippet saved the day. Thanks!
  • John
    John over 3 years
    @DanielBrückner Your answer removes entire elements if they are 1 letter tags such as <b>. Instead you should use <[^>].*?>.
  • anhtv13
    anhtv13 over 3 years
    Anyone got the exception "Illegal characters in path." when the debug runs to the line doc.LoadHtml?
  • Ben.S
    Ben.S almost 3 years
    the link for the HTML-AGILITY-PACK will be broken soon (1 July 2021) so this is the new one html-agility-pack.net/?z=codeplex
  • Ruslan
    Ruslan about 2 years
    I am wondering why do we need to specify the "??" and "" characters in doc.LoadHtml()? I tried without these characters and the method did not work for me.