日期:2014-05-17  浏览次数:20700 次

求高手写一个正则
原字符串类似于这样
有图片链接的a标签,有文字的标签
HTML code

<a href="1.html"><img src ..></a> <a href="1.html">文字</a><a href="2.html"><img src ..></a> <a href="2.html">文字</a>
<a href="3.html"><img src ..></a> <a href="3.html">文字</a>




我想取出有图片的那几组字符串

我使用的正则为 (?i)<a[\s\S]*?><img[\s\S]*?</a>

取的第一个还正确,但取第二个的时候就是得到这些字符串了
HTML code

<a href="1.html">文字</a><a href="2.html"><img src ..></a>



请高手帮帮忙,感激不尽,

------解决方案--------------------
C# code

(?is)<a[^>]*><img(?><a[^>]*>(?<Open>)|</a>(?<-Open>)|(?:(?!</?a\b).)*)*(?(Open)(?!))</a>
试试

------解决方案--------------------
Regex reg = new Regex(@"(?is)(?<=<img)[^>]*?(?=>)");
------解决方案--------------------
(?i)<a\b[^>]*?><img\b[^>]*?></a>
------解决方案--------------------
C# code
Regex re = new Regex(@"<a[^>]*><img[^>]*></a>", RegexOptions.Singleline);
MatchCollection mc = re.Matches("text");
foreach (Match ma in mc)
{
}