public string GetHref(string HtmlCode)
{
string MatchVale = "";
string Reg = @"(h|H)(r|R)(e|E)(f|F) *= *('|"")?((\w|\\|\/|\.|:|-|_)+)('|""| *|>)?";
foreach (Match m in Regex.Matches(HtmlCode, Reg))
{ MatchVale += (m.Value).ToLower().Replace("href=", "").Replace("\"", "").Trim() + "<br />"; }
return MatchVale;
}以上是提取网页超链接的正则,现在无法处理以下JS链接的特殊情况,那位麻烦给看下改下呀?
<A href=# onClick="javascript:window.open('zb_prev.asp?id=2008723163932160','', 'toolbars=0, scrollbars=yes, location=0, statusbars=0, menubars=0, resizable=1,width=750, height=570, left = 150, top = 100')"> 郑州:体育馆楼 </A>最好是改成兼容最上面的那个,实在不行单独写一个也可以
{
string MatchVale = "";
string Reg = @"(h|H)(r|R)(e|E)(f|F) *= *('|"")?((\w|\\|\/|\.|:|-|_)+)('|""| *|>)?";
foreach (Match m in Regex.Matches(HtmlCode, Reg))
{ MatchVale += (m.Value).ToLower().Replace("href=", "").Replace("\"", "").Trim() + "<br />"; }
return MatchVale;
}以上是提取网页超链接的正则,现在无法处理以下JS链接的特殊情况,那位麻烦给看下改下呀?
<A href=# onClick="javascript:window.open('zb_prev.asp?id=2008723163932160','', 'toolbars=0, scrollbars=yes, location=0, statusbars=0, menubars=0, resizable=1,width=750, height=570, left = 150, top = 100')"> 郑州:体育馆楼 </A>最好是改成兼容最上面的那个,实在不行单独写一个也可以
上面这个表达式取出window.open('zb_prev.asp?id=2008723163932160'这部分,你仍然需要替代window.open 左括号 和两个单引号,需要三次操作,最后去空格,跟你的操作一样,主要避免空格的影响
再用
GetHref(string HtmlCode)
string src = "<a href=\"http://www.csdn/p1.pdf\">PDF</a><a href='abc.jpef'>aaa </a>";
src += "<A href=# onClick=\"javascript:window.open('zb_prev.asp?id=2008723163932160','', 'toolbars=0, scrollbars=yes, location=0, statusbars=0, menubars=0, resizable=1,width=750, height=570, left = 150, top = 100')\"> 郑州:体育馆楼 </A>";
src += "<a href=\"http://www.csdn/p2.pdf\">PDF</a><a href=\"http://www.csdn/p3.pdf\">PDF</a>";
MatchCollection mc = Regex.Matches(src, @"(?<=(href=)|(window\.open\())['""]([^'""]+)['""]", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
result += m.Value + "\r\n";
}