日期:2014-05-18  浏览次数:20421 次

正则表达式解析html中的href标记的所有格式 急!!急!!急!!
本人想解析出html中的href标记的所有格式和 <a> </a> 之间的文本标题:
例如: <a   href= ' '> fff </a>   或者是 <a   href= " "> fff </a>   或者是 <a   href=> fff </a>
以上是我能想到的连接的格式了,我想用正则表达式解析出
href的连接地址,还有 <a> </a> 之间的文本标题,例子中的fff
请问这个正则表达式应该怎么写啊!急

------解决方案--------------------
interStr=@ "(? <= <a [^> ]*> ).*(?= </a> ) ";
------解决方案--------------------
string s = "........... ";

Regex re = new Regex(@ " <a[^> ]*href=( " "(? <href> [^ " "]*) " "| '(? <href> [^ ']*) '|(? <href> [^\s> ]*))[^> ]*> (? <text> .*?) </a> ", RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = re.Match(s);
if (m.Success)
{
string link = m.Groups[ "href "].Value;
string text = Regex.Replace(m.Groups[ "text "].Value, " <[^> ]*> ", " ");
Console.WriteLine( "link:{0}\ntext:{1} ", link, text);
}
------解决方案--------------------
try


string yourStr = ..........;
MatchCollection mc = Regex.Matches(yourStr, @ " <a[^> ]*href=([ ' " "]?)(? <url> [^ ' " "> \s]*)\1?[^> ]*> (? <text> [^ <]*) </a> ", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
Response.Write(m.Groups[ "url "].Value + " <br> ");
Response.Write(m.Groups[ "text "].Value + " <br> ");
}
------解决方案--------------------
如果 <a...> 和 </a> 之间可能有其它html标签,那正则改成下面的

MatchCollection mc = Regex.Matches(yourStr, @ " <a[^> ]*href=([ ' " "]?)(? <url> [^ ' " "> \s]*)\1?[^> ]*> (? <text> [\s\S]*?) </a> ", RegexOptions.IgnoreCase);