-2

Possible Duplicate:
Using C# regular expressions to remove HTML tags
Extract Content from Div Tag C# RegEx

Hi and thanks for looking!

Question

How do I write a regular expression (in C#) to reduce this string:

<span class='foo'>bar</span>

To this:

bar

In a severe time crisis, so please excuse my not RingTFM! Thanks!

Matt

Community
  • 1
  • 1
Matt Cashatt
  • 22,420
  • 27
  • 75
  • 108
  • Do you want a regular expression for that exact string (if so why) or a class of strings which that is an example of? replacing `.*` with `bar` will satisfy your requirements but I am sure the requirements are stronger than that so you should try to describe your problem in more detail. – Chris Feb 17 '12 at 17:00
  • 1
    Especially when you're in "a a severe time crisis" you should take the time to detail the question correctly. The current one is answerable with `return "bar";` – Henk Holterman Feb 17 '12 at 17:03
  • Thanks for all of your concern fellas, but Joey answered the question with just what I needed within seconds of posting and without further detail. That guy must be really smart. Thanks again though! – Matt Cashatt Feb 17 '12 at 17:14

3 Answers3

1
Regex.Replace(myString, "<[^>]*>", "");
Joey
  • 330,812
  • 81
  • 665
  • 668
1

Taken from another question.

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

But notice these warnings.

Community
  • 1
  • 1
raveturned
  • 2,546
  • 26
  • 29
1

You could also get the same result with:

XDocument.Parse("<span class='foo'>bar</span>").Element("span").Value
Aaron Anodide
  • 16,594
  • 15
  • 60
  • 119