在.NET中处理正则表达式时,我有两个选择:
-
检查模式匹配的字符串:
<a ([^>]*?)href="http://the.site.com/photo/[0-9]*">
-
捕获图案的一部分:
<a ([^>]*?)href="http://the.site.com/photo/(?<photoname>.*?)">
但是,如果我想检查模式匹配并捕获与单个正则表达式匹配的部件怎么办?
只需在捕获时使用它:
<a ([^>]*?)href="http://the.site.com/photo/(?<photoname>[0-9]+)">
使用 htmlAgilityPack
HtmlDocument doc = new HtmlDocument();
doc.Load(htmlUrl);
var pattern=@"^(?<=https?://the.site.com/photo/)d+$";
var hrefList= doc.DocumentNode
.SelectNodes("//a[@href]")
.Select(p =>p.Attributes["href"].Value)//select all hrefs
.Where(p => Regex.IsMatch(p,pattern))//filter href
.Select(p=>Regex.Match(p,pattern).Value);//select required digits
好的先生,您可以使用一种模式匹配并捕获到一个命名组中!
<a (?:[^>]*?)hrefs*?=s*"http://the.site.com/photo/(?<photoname>[0-9]+)"
名为 photoname
的组将包含所需的捕获。
即使href
不是a
元素上的第一个属性,此正则表达式也将起作用。它还将忽略任意空格。