public static string[] GetHtmlImageUrlList(string sHtmlText) { // 定义正则表达式用来匹配 img 标签 Regex regImg = new Regex(@"<input\b[^<>]*?\bsrc=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<inputValue>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); // 搜索匹配的字符串 MatchCollection matches = regImg.Matches(sHtmlText); int i = 0; string[] sUrlList = new string[matches.Count]; // 取得匹配项列表 foreach (Match match in matches) sUrlList[i++] = match.Groups["inputValue"].Value; return sUrlList; }
(?<inputValue>[^\s\t\r\n""'<>]*)和match.Groups["inputValue"].Value;来怎么理解,能否在举个就简单例子
首先,你有个笔误:第一个input应该是img,这样才匹配的是img标签;
然后,(?<inputValue>[^\s\t\r\n""'<>]*)的意思是匹配并捕获src的值,并给它取个别名叫inputValue,这样如果匹配到的话,你有可以通过match.Groups["inputValue"].Value来取值,这是命名捕获组。另外,还有一种非命名捕获组,是通过捕获组的位置来取值的,比如要匹配www.baidu.com中的baidu,我们可以使用^www\.([^.]+)\.com$来匹配,这里的([^.]+)就是非命名捕获组,你可以使用match.Groups[1].Value来获取,具体的位置约定可以百度
了解了。