匹配所有内容的正则表达式,除了 HTML 标签 [英] Regular expression to match everything, except HTML tags

查看:42
本文介绍了匹配所有内容的正则表达式,除了 HTML 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Di, 12.04.161DD<td>255</td><td>ABC</td><tr>

我只想匹配 ABC 或其他任何介于

之间的内容

</td>(ABC前后)

这个模式对我不起作用:

((?![DM][ir],['][0-3][0-9]\\.[0-1][0-9]\\.[0-9][0-9]</td><td>[1-9][0-2]?</td><td>[AZ]?[AZ]?[AZ]?[AZ]?[1-5]?</td><td>(---|[AZ]?[AZ]?[AZ]?[AZ]?[1-5]?)</td><td>).*(?!</td></tr>))

你有什么想法吗?感谢帮助

解决方案

正如 Amy 所说,不要使用正则表达式来解析 HTML.您可以从 NuGet 安装

<tr><td>Di, 12.04.16</td><td>1</td><td>D</td><td>D</td><td>255</td><td>ABC</td><tr>

I want to only match ABC or anything else that stand between

<td>
</td> (before and after ABC)

This Patter doesnt work for me:

((?!<tr><td>[D-M][i-r],[' ][0-3][0-9]\\.[0-1][0-9]\\.[0-9][0-9]</td><td>[1-9][0-2]?</td><td>[A-Z]?[A-Z]?[A-Z]?[A-Z]?[1-5]?</td><td>(---|[A-Z]?[A-Z]?[A-Z]?[A-Z]?[1-5]?)</td><td>).*(?!</td></tr>))

Do you have any idea? Thx for help

解决方案

As Amy said, don't use regex to parse HTML. You can install Html Agility Pack from NuGet and use System.Linq Namespace to parse it.

For example here:

string html = "<html><head></head><body><p class='testclass'>This is a paragraph.</p><table><tr><td>Di, 12.04.16</td><td>1</td><td>D</td><td>D</td><td>255</td><td>ABC</td><tr></table></body></html>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var programmes = doc.DocumentNode.Descendants().Where(d => d.GetAttributeValue("class", "") == "testclass");
var trs = doc.DocumentNode.Descendants("tr"); // Give you all the trs
foreach (var tr in trs)
{
    var tds = tr.Descendants("td").ToArray(); // Get all the tds
    //Sample, show the result in a TextBlock
    foreach (var td in tds)
    {
        txt.Text = txt.Text + " " + td.InnerText;
    }
}

The result is so:

这篇关于匹配所有内容的正则表达式,除了 HTML 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆