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.0 by 1 people
- Currently 4/5 Stars.
- 1
- 2
- 3
- 4
- 5