C# 正则表达式:从超链接获取url值
我有一个包含html的字符串。我想使用C从超链接获取所有href值。 目标字符串 我想获得值~/abc/cde和~/abc/ghq使用解析HTML。就在他们的页面上,他们有一个解析href值的一些HTML的示例:C# 正则表达式:从超链接获取url值,c#,regex,C#,Regex,我有一个包含html的字符串。我想使用C从超链接获取所有href值。 目标字符串 我想获得值~/abc/cde和~/abc/ghq使用解析HTML。就在他们的页面上,他们有一个解析href值的一些HTML的示例: foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"]) { HtmlAttribute att = link["href"]; // Do stuff with attribut
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
HtmlAttribute att = link["href"];
// Do stuff with attribute value
}
使用用于解析HTML的。就在他们的页面上,他们有一个解析href值的一些HTML的示例:
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
HtmlAttribute att = link["href"];
// Do stuff with attribute value
}
使用正则表达式解析HTML是不可取的,请考虑注释中的文本等 也就是说,下面的正则表达式应该可以做到这一点,如果需要,还可以在标记中提供链接HTML:
Regex regex = new Regex(@"\<a\s[^\<\>]*?href=(?<quote>['""])(?<href>((?!\k<quote>).)*)\k<quote>[^\>]*\>(?<linkHtml>((?!\</a\s*\>).)*)\</a\s*\>", RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
for (Match match = regex.Match(inputHtml); match.Success; match=match.NextMatch()) {
Console.WriteLine(match.Groups["href"]);
}
使用正则表达式解析HTML是不可取的,请考虑注释中的文本等 也就是说,下面的正则表达式应该可以做到这一点,如果需要,还可以在标记中提供链接HTML:
Regex regex = new Regex(@"\<a\s[^\<\>]*?href=(?<quote>['""])(?<href>((?!\k<quote>).)*)\k<quote>[^\>]*\>(?<linkHtml>((?!\</a\s*\>).)*)\</a\s*\>", RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture);
for (Match match = regex.Match(inputHtml); match.Success; match=match.NextMatch()) {
Console.WriteLine(match.Groups["href"]);
}
下面是regex use IgnoreWhitespace选项的一个片段:
(?:<)(?<Tag>[^\s/>]+) # Extract the tag name.
(?![/>]) # Stop if /> is found
# -- Extract Attributes Key Value Pairs --
((?:\s+) # One to many spaces start the attribute
(?<Key>[^=]+) # Name/key of the attribute
(?:=) # Equals sign needs to be matched, but not captured.
(?([\x22\x27]) # If quotes are found
(?:[\x22\x27])
(?<Value>[^\x22\x27]+) # Place the value into named Capture
(?:[\x22\x27])
| # Else no quotes
(?<Value>[^\s/>]*) # Place the value into named Capture
)
)+ # -- One to many attributes found!
这将为您提供每个标记,您可以筛选出所需的内容,并针对所需的属性
我在我的博客中写了更多关于这方面的内容。这里是regex use IgnoreWhitespace选项的一个片段:
(?:<)(?<Tag>[^\s/>]+) # Extract the tag name.
(?![/>]) # Stop if /> is found
# -- Extract Attributes Key Value Pairs --
((?:\s+) # One to many spaces start the attribute
(?<Key>[^=]+) # Name/key of the attribute
(?:=) # Equals sign needs to be matched, but not captured.
(?([\x22\x27]) # If quotes are found
(?:[\x22\x27])
(?<Value>[^\x22\x27]+) # Place the value into named Capture
(?:[\x22\x27])
| # Else no quotes
(?<Value>[^\s/>]*) # Place the value into named Capture
)
)+ # -- One to many attributes found!
这将为您提供每个标记,您可以筛选出所需的内容,并针对所需的属性
我在我的博客中写了更多关于这方面的内容。:@balpha:什么?这在这里绝对不适用。您可以使用正则表达式获取打开标记的href,甚至不用担心关闭标记。@Platinum:@balpha:好吧,我很高兴您有幽默感,但考虑到它也出现在下面的每个答案中,您可以理解为什么我可能会认为人们有这种下意识的omg从不使用正则表达式解析HTML响应,emoticon或no.@Platinum Azure:没什么害处-我只是想提一下这个答案,因为如果你读过一次,它就会一直萦绕在你的脑海中,每当你开始用正则表达式解析标记时,它就会萦绕在你的脑海中。这并不意味着它总是错的,但是在你的头脑中有了这个答案至少会让你想一想。我有时也会在没有真正的解析器的情况下分析HTML,但我通常会在它前面放一条中心无法保存的注释:@balpha:What?这在这里绝对不适用。您可以使用正则表达式获取打开标记的href,甚至不用担心关闭标记。@Platinum:@balpha:好吧,我很高兴您有幽默感,但考虑到它也出现在下面的每个答案中,您可以理解为什么我可能会认为人们有这种下意识的omg从不使用正则表达式解析HTML响应,emoticon或no.@Platinum Azure:没什么害处-我只是想提一下这个答案,因为如果你读过一次,它就会一直萦绕在你的脑海中,每当你开始用正则表达式解析标记时,它就会萦绕在你的脑海中。这并不意味着它总是错的,但是在你的头脑中有了这个答案至少会让你想一想。我有时也会在没有真正的解析器的情况下分析HTML,但我通常会在它前面放一条中心无法保存的注释:这正是我所要寻找的,群组的工作原理是什么?我正在为img src做同样的尝试,但它不起作用,知道吗?正则表达式srcs=新正则表达式@\[']\k、 *\k[^\>]*\>??!\.\\\\,RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;img标记是空标记,因此您没有内容。试试这个:\[']\k、 *\k[^\>]*\>这正是我想要的,小组的工作原理?我正在为img src尝试同样的方法,但它不起作用,知道吗?正则表达式srcs=新正则表达式@\[']\k、 *\k[^\>]*\>??!\.\\\\,RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;img标记是空标记,因此您没有内容。试试这个:\[']\k、 *\k[^\>]*\>