---
title: "Strip HTML tags from a string using regular expressions"
url: "https://osherove.com/blog/2003/5/13/strip-html-tags-from-a-string-using-regular-expressions.md"
canonical: "https://osherove.com/blog/2003/5/13/strip-html-tags-from-a-string-using-regular-expressions.html"
published: "2003-05-13T06:41:00"
updated: "2003-05-13T06:41:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "Paschal asked me to find a simple solution for stripping HTML tags from a given string using Regular expressions. The solution is quite simple: 1. Retrieve all the HTML tags using this pattern: <(.|\\n)*?> 2. Replace…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Strip HTML tags from a string using regular expressions

By Roy Osherove May 13, 2003 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published May 13, 2003; technical details may be outdated.

Paschal [asked me](http://dotnetweblogs.com/pleloup/posts/5611.aspx) to find a simple solution for stripping HTML tags from a given string using Regular expressions.

The solution is quite simple:

1\. Retrieve all the HTML tags using this pattern: **<(.|\\n)\*?>**

2\. Replace them with an empty string and return the result

Here's a C# function that does this:

private string StripHTML(string htmlString)

{

//This pattern Matches everything found inside html tags;

//(.|\\n) - > Look for any character or a new line

// \*?  -> 0 or more occurences, and make a non-greedy search meaning

//That the match will stop at the first available '>' it sees, and not at the last one

//(if it stopped at the last one we could have overlooked

//nested HTML tags inside a bigger HTML tag..)

// Thanks to [Oisin](http://www.boardex.com/) and [Hugh Brown](http://www.iwebthereforeiam.com/) for helping on this one...

string pattern = @"<(.|\\n)\*?>";

return  Regex.Replace(htmlString,pattern,string.Empty);

}

Or with just one line of code:

string

stripped = Regex.Replace(textBox1.Text,@"<(.|\\n)\*?>",string.Empty);

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- May 15, 2003

    ### [System.Text.Encoding.Tip](https://osherove.com/blog/2003/5/15/systemtextencodingtip.html)

- May 14, 2003

    ### [A Long Day!](https://osherove.com/blog/2003/5/14/a-long-day.html)

- May 12, 2003

    ### [June Is Officially here](https://osherove.com/blog/2003/5/12/june-is-officially-here.html)

- May 12, 2003

    ### [New Article: Practical Parsing Using Groups in Regular Expressions](https://osherove.com/blog/2003/5/12/new-article-practical-parsing-using-groups-in-regular-expres.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
