Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET regex inner text between td, span, a tag

Tags:

c#

regex

<table >
    <tr>
        <td colspan="2" style="height: 14px">
            tdtext1
            <a>hyperlinktext1<a/> 
        </td>
    </tr>
    <tr>
        <td>
            tdtext2
        </td>
        <td>
            <span>spantext1</span>
        </td>
    </tr>
</table>   

This is my sample text. How to write a regular expression in C# to get the matches for the innertext for td, span, hyperlinks.

like image 831
mushtaqck Avatar asked Dec 13 '25 07:12

mushtaqck


2 Answers

I cringe every time I hear the words regex and HTML in the same sentence. I would suggest checking out the HtmlAgilityPack on CodePlex which is a very tolerant HTML parser that lets you use XPath queries against the parsed document. It's much cleaner and the person that inherits your code will thank you!

EDIT

As per the comments below, here's some examples of how to get the InnerText of those tags. Very simple.

var doc = new HtmlDocument();
doc.LoadHtml("...your sample html...");

// all <td> tags in the document
foreach (HtmlNode td in doc.DocumentNode.SelectNodes("//td")) {
    Console.WriteLine(td.InnerText);
}

// all <span> tags in the document
foreach (HtmlNode span in doc.DocumentNode.SelectNodes("//span")) {
    Console.WriteLine(span.InnerText);
}

// all <a> tags in the document
foreach (HtmlNode a in doc.DocumentNode.SelectNodes("//a")) {
    Console.WriteLine(a.InnerText);
}
like image 97
Josh Avatar answered Dec 14 '25 20:12

Josh


        static void Main(string[] args)
    {
        //...
       // using (WebClient client = new WebClient()) // WebClient class inherits IDisposable
       // {
        HtmlDocument doc = new HtmlWeb().Load("http://www.freeclup.com");

            foreach (HtmlNode span in doc.DocumentNode.SelectNodes("//span"))
            {
                Console.WriteLine(span.InnerText);
            }
            Console.ReadKey();

      //  }
    }
like image 34
MAG TOR Avatar answered Dec 14 '25 21:12

MAG TOR