【发布时间】:2010-09-09 19:50:14
【问题描述】:
我在 c# 中有一个正则表达式,用于匹配图像标签并提取 URL。我的代码在大多数情况下都有效。下面的代码会将所有相对图像 URL “修复”为绝对 URL。
问题是正则表达式与以下内容不匹配:
<img height="150" width="202" alt="" src="../Image%20Files/Koala.jpg" style="border: 0px solid black; float: right;">
例如它匹配这个就好了
<img height="147" width="197" alt="" src="../Handlers/SignatureImage.ashx?cid=5" style="border: 0px solid black;">
任何关于如何使其匹配的想法都会很棒。我认为问题在于 % 但我可能是错的。
Regex rxImages = new Regex(" src=\"([^\"]*)\"", RegexOptions.IgnoreCase & RegexOptions.IgnorePatternWhitespace);
mc = rxImages.Matches(html);
if (mc.Count > 0)
{
Match m = mc[0];
string relitiveURL = html.Substring(m.Index + 6, m.Length - 7);
if (relitiveURL.Substring(0, 4) != "http")
{
Uri absoluteUri = new Uri(baseUri, relitiveURL);
ret += html.Substring(0, m.Index + 5);
ret += absoluteUri.ToString();
ret += html.Substring(m.Index + m.Length - 1, html.Length - (m.Index + m.Length - 1));
ret = convertToAbsolute(URL, ret);
}
}
【问题讨论】:
标签: c# regex url pattern-matching