使用分组,模式替换,反向引用,.NET下研究正则的一个好例子.
string input = @"
<a href=""http://www.123456.com/333x/666e3/aaa.jpg"">点此查看图片 </a>
<div class=""greenLight"">
<a href=""http://www.123456.com/333x/666e3/aaa.html?key=google"" target=""_blank"" >点此查看网站 </a>
</div>
<a href=""http://www.123456.com/333x/666e3/bbb.jpg"">点此查看图片 </a>
<div class=""greenLight"">
<a href=""http://www.123456.com/333x/666e3/bbb.html?key=google"" target=""_blank"" >点此查看网站 </a>
</div>
<a href=""http://www.123456.com/333x/666e3/ccc.jpg"">点此查看图片 </a>
<div class=""greenLight"">
<a href=""http://www.123456.com/333x/666e3/ccc.html?key=google"" target=""_blank"" >点此查看网站 </a>
</div>";
MatchCollection ms = Regex.Matches(input, @"<a/s+href=""(?<img>.*?)"">(.*?)</a>/s*<div/s+class=""(?<class>.*?)"">/s*"
+@"<a/s+href=""(?<url>.*?)""/s*target=""(.*?)""/s*>(.*?)</a>/s*</div>");
foreach (Match match in ms)
{
Console.WriteLine("img={0}", match.Result("${img}"));
Console.WriteLine("url={0}", match.Result("${url}"));
Console.WriteLine("*******************************");
}
Console.ReadKey();