Extract the contents of a string between two string delimiters using match in C#

旧街凉风 提交于 2019-12-25 10:00:39

问题


So, say I'm parsing the following HTML string:

<html>
    <head>
        RANDOM JAVASCRIPT AND CSS AHHHHHH!!!!!!!!
    </head>
    <body>
        <table class="table">
            <tr><a href="/subdir/members/Name">Name</a></tr>
            <tr><a href="/subdir/members/Name">Name</a></tr>
            <tr><a href="/subdir/members/Name">Name</a></tr>
            <tr><a href="/subdir/members/Name">Name</a></tr>
            <tr><a href="/subdir/members/Name">Name</a></tr>
            <tr><a href="/subdir/members/Name">Name</a></tr>
            <tr><a href="/subdir/members/Name">Name</a></tr>
            <tr><a href="/subdir/members/Name">Name</a></tr>
            <tr><a href="/subdir/members/Name">Name</a></tr>
            <tr><a href="/subdir/members/Name">Name</a></tr>
        </table>
    <body>
</html>

and I want to isolate the contents of ** (everything inside of the table class)

Now, I used regex to accomplish this:

string pagesource = (method that extracts the html source and stores it into a string);
string[] splitSource = Regex.Split(pagesource, "<table class=/"member/">;
string memberList = Regex.Split(splitSource[1], "</table>");
//the list of table members will be in memberList[0];
//method to extract links from the table
ExtractLinks(memberList[0]);

I've been looking at other ways to do this extraction, and I came across the Match object in C#.

I'm attempting to do something like this:

Match match = Regex.Match(pageSource, "<table class=\"members\">(.|\n)*?</table>");

The purpose of the above was to hopefully extract a match value between the two delimiters, but, when I try to run it the match value is:

match.value = </table>

MY question, as such, is: is there a way to extract data from my string that is slightly easier/more readable/shorter than my method using regex? For this simple example, regex is fine, but for more complex examples, I find myself with the coding equivalent of scribbles all over my screen.

I would really like to use match, because it seems like a very neat and tidy class, but I can't seem to get it working for my needs. Can anyone help me with this?

Thank you very much!


回答1:


Use an HTML parser, like HTML Agility Pack.

var doc = new HtmlDocument();

using (var wc = new WebClient())
using (var stream = wc.OpenRead(url))
{
    doc.Load(stream);
}

var table = doc.DocumentElement.Element("html").Element("body").Element("table");
string tableHtml = table.OuterHtml;



回答2:


You can use XPath with the HTmlAgilityPack:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(s);
var elements = doc.DocumentNode.SelectNodes("//table[@class='table']");

foreach (var ele in elements)
{
    MessageBox.Show(ele.OuterHtml);
}



回答3:


You have add parenthesis in the regular expression in order to capture the matches:

Match match = Regex.Match(pageSource, "<table class=\"members\">(.|\n*?)</table>");

Anyways it seems that only Chuck Norris can parse HTML with regex correctly.



来源:https://stackoverflow.com/questions/11015527/extract-the-contents-of-a-string-between-two-string-delimiters-using-match-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!