7

I have the HTML code of a webpage in a text file. I'd like my program to return the value that is in a tag. E.g. I want to get "Julius" out of

<span class="hidden first">Julius</span>

Do I need regular expression for this? Otherwise what is a string function that can do it?

2
  • 1
    You do not want regex. HTML is too complex for regex parsing. Here is the infamous answer to that point : stackoverflow.com/questions/1732348/… Commented Nov 5, 2012 at 14:45
  • 1
    Also what do you actually want? Assuming you don't just want "Julius" returned everytime do you want all text between <span> tags? All text between <span> tags that have a class of "first"? Commented Nov 5, 2012 at 14:46

4 Answers 4

14

You should be using an html parser like htmlagilitypack .Regex is not a good choice for parsing HTML files as HTML is not strict nor is it regular with its format.

You can use below code to retrieve it using HtmlAgilityPack

HtmlDocument doc = new HtmlDocument();
doc.Load(yourStream);

var itemList = doc.DocumentNode.SelectNodes("//span[@class='hidden first']")//this xpath selects all span tag having its class as hidden first
                  .Select(p => p.InnerText)
                  .ToList();

//itemList now contain all the span tags content having its class as hidden first
Sign up to request clarification or add additional context in comments.

2 Comments

No, the C#/.NET regex engine is certainly capable of matching non-REGULAR expressions. But you are correct that other tools are a better choice for parsing HTML.
@ridgerunner u r right..i guess .net has the best regex engine..:D
7

I would use the Html Agility Pack to parse the HTML in C#.

Comments

2

I'd strongly recommend you look into something like the HTML Agility Pack

Comments

1

i've asked the same question few days ago and ened up using HTML Agility Pack, but here is the regular expressions that you want

this one will ignore the attributes

<span[^>]*>(.*?)</span>

this one will consider the attributes

<span class="hidden first"[^>]*>(.*?)</span>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.