private String getHref(string a)
{
try
{
string p = @"href\s*=\s*('[^']*'|""[^""]*""|\S+\s+)";
MatchCollection matches = Regex.Matches(a, p,
RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
foreach (Match m in matches) return m.Value;
return null;
}
catch (Exception e) { throw e; }
}
三種格式:
href="https://..."href='https://...'href=https://...
private String getUrl(string href)
{
try
{
if (href == null) return href;
int n = href.IndexOf('=');
String s = href.Substring(n + 1);
int begin = 0, end = 0;
string sign = "";
if (s.Contains("\"")) sign = "\"";
else if (s.Contains("'")) sign = "'";
else return getFullUrl(s.Trim());
begin = s.IndexOf(sign);
end = s.LastIndexOf(sign);
return getFullUrl(s.Substring(begin + 1, end - begin - 1).Trim());
}
catch (Exception e) { throw e; }
}
再透過 getFullUrl 處理相對路徑(見上篇)。