C#中锚html标签的正则表达式?

moo*_*ara 2 c# regex

我需要在C#中使用正则表达式作为html源代码中的锚标记尽可能通用.考虑这个HTML代码:

<a id="[constant]"
      href="[specific]"
    >GlobalPlatform Card Specification 2.2
    March, 2006</a>
Run Code Online (Sandbox Code Playgroud)

通过[常量]我的意思是该值是一个常量字符串,所以它没有问题.通过[具体]我的意思是地址是一个简单而具体的字符串,因此它的正则表达式很简单.主要问题是我无法处理锚标签中间的换行符.之前我写过这个正则表达式,除了处理锚标记的标题之间的换行符之外,效果很好.

<a[\\s\\n\\r]+id=\"[constant]"[\\s\\n\\r]+href=\"[specific]"[\\s\\n\\r]*>[\\s\\n\\r]*[^\\n\\r]+[\\s\\n\\r]*</a>
Run Code Online (Sandbox Code Playgroud)

请帮我

Joã*_*elo 6

在解析HTML时,您应该远离正则表达式,并使用HTML Agility Pack之类的HTML解析器.

并且为了帮助您开始检查解析单个锚标记是多么简单.

HtmlDocument doc = new HtmlDocument();

doc.LoadHtml(@"<a id=""[constant]""
      href=""[specific]""
    >GlobalPlatform Card Specification 2.2
    March, 2006</a>
");

var anchor = doc.DocumentNode.Element("a");

Console.WriteLine(anchor.Id);
Console.WriteLine(anchor.Attributes["href"].Value);
Run Code Online (Sandbox Code Playgroud)

击败正则表达式,你不觉得吗?:)