-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
  • 23,490
  • 28
  • 78
  • 111
  • 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";` – H H 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
  • 344,408
  • 85
  • 689
  • 683
1

Taken from another question.

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

But notice these warnings.

Community
  • 1
  • 1
raveturned
  • 2,637
  • 24
  • 30
1

You could also get the same result with:

XDocument.Parse("<span class='foo'>bar</span>").Element("span").Value
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121