日期:2014-05-20  浏览次数:20416 次

求一正则
<em>[<a href="">经验窍门</a>]</em><a href="thjy.co" onClick="atarget(this)" class="xst">sfasdf</a>
<img src="static/image/common/hot_3.gif" alt="heatlevel" title="3 级热门" align="absmiddle">
 <em>[<a href="">经验窍门</a>]</em><a href="bbs.thjy.co" onClick="atarget(this)" class="xst">sfasdf</a>
<img src="static/image/common/hot_3.gif" alt="heatlevel" title="3 级热门" align="absmiddle">
 <em>[<a href="">经验窍门</a>]</em><a href="test.thjy.co" onClick="atarget(this)" class="xst">sfasdf</a>
<img src="static/image/common/hot_3.gif" alt="heatlevel" title="3 级热门" align="absmiddle">

把带有onClick="atarget(this)" 的a标签全部取出来结果如下:
thjy.co
bbs.thjy.co
test.thjy.co 

那位高手解答下谢谢




------解决方案--------------------
C# code
            string str = @"<em>[<a href="""">经验窍门</a>]</em><a href=""thjy.co"" onClick=""atarget(this)"" class=""xst"">sfasdf</a>
<img src=""static/image/common/hot_3.gif"" alt=""heatlevel"" title=""3 级热门"" align=""absmiddle"">
 <em>[<a href="""">经验窍门</a>]</em><a href=""bbs.thjy.co"" onClick=""atarget(this)"" class=""xst"">sfasdf</a>
<img src=""static/image/common/hot_3.gif"" alt=""heatlevel"" title=""3 级热门"" align=""absmiddle"">
 <em>[<a href="""">经验窍门</a>]</em><a href=""test.thjy.co"" onClick=""atarget(this)"" class=""xst"">sfasdf</a>
<img src=""static/image/common/hot_3.gif"" alt=""heatlevel"" title=""3 级热门"" align=""absmiddle"">";
            Regex reg = new Regex(@"(?i)<a\s*href=""(?<url>[^""]+)""\s+onclick=""atarget\(this\)""");
            MatchCollection mc = reg.Matches(str);
            foreach (Match m in mc)
            {
                Console.WriteLine(m.Groups["url"].Value);
            }

------解决方案--------------------
Regex reg = new Regex(@"(?i)<a[^>]*?href=(['""\s]?)(?<url>[^\s'""]+)\1\s+onclick=(['""\s]?)atarget\(this\)\2");