Regular Expressions - Everyday use with System.Text.RegularExpressions.Regex.Replace()

April 10, 2008 09:28 by nat

I recently had to display some text on a page where the source field was fully marked up html, but I only wanted the content. I imagined a long drawn out process of doing a string.Replace() on all html tags, and was considering booking a holiday, when REGEX came to the rescue. It was so simple - much simpler than I ever thought it would be.

Using the expression "<.*?>" to match all html tags, i then called the System.Text.RegularExpressions.Regex.Replace() method, which is very similar to string.Replace, only you can pass a regular expression to replace only the matches, and voila - all the html markup is removed.

Simple now that I think of it, but before I had seen this used, this task seemed a real pain in the behind.

A couple of useful regex resources:
http://www.quanetic.com/regex.php - on online REGEX testing tool
http://www.regular-expressions.info/reference.html - REGEX reference site - from basic to complex, everything you need.

Hope this comes in handy...

Nat


Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Add comment


 

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

July 31. 2010 20:24

Calendar

July 2010
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567