正则表达式提取字符串仅包含跨度标签之间的 img 标签

Regex extract string only contains img tag between span tags

我要求只获取在 anchor 和 span 标签下的图像。 有人可以提供获取图像的解决方案吗? 提前致谢。

详情: 示例代码 link: http://regexr.com/v1?38u5g

正则表达式 1:<a (.*?)><span (.*?)>(.*?)<\/span><\/a>

正则表达式 2:<a (.*?)><span (.*?)><img(.*?) \/><\/span><\/a>

示例代码:

 <span style="font-size: 1.2em;">We voted for our next selection as well. The choice was between Joan Didion's </span><a href="http://www.randomhouse.com/knopf/catalog/results2.pperl?authorid=7051"><span style="font-size: 1.2em;">The Year of Magical Thinking</span></a><span style="font-size: 1.2em;">, Sarah Dunant's </span><a href="http://www.randomhouse.com/catalog/display.pperl?isbn=9781588365507"><span style="font-size: 1.2em;">In the Company of the Courtesan</span></a><span style="font-size: 1.2em;">, </span><a href="http://www.jodipicoult.com/"><span style="font-size: 1.2em;">Jodi Picoult's</span></a> <a href="http://www.simonsays.com/content/book.cfm?tab=1&amp;pid=504542"><span style="font-size: 1.2em;">My Sister's Keeper</span></a><span style="font-size: 1.2em;">, Jhumpa Lahiri's </span><a href="http://http//www.houghtonmifflinbooks.com/catalog/titledetail.cfm?titleNumber=694004"><span style="font-size: 1.2em;">The Namesake</span></a><span style="font-size: 1.2em;"> and </span><a href="http://www.randomhouse.com/catalog/display.pperl?isbn=9780385721813&amp;view=rg"><span style="font-size: 1.2em;">When the Emperor Was Divine</span></a><span style="font-size: 1.2em;"> by Julie Otsuka. </span><a href="http://bookclubgirl.typepad.com/photos/uncategorized/2007/04/19/joan_didion.jpg"><span style="font-size: 1.2em;"><img style="float: left; margin: 0px 5px 5px 0px;" title="Joan_didion" src="joan_didion.jpg" alt="Joan_didion" width="100" height="88" border="0" /></span></a><span style="font-size: 1.2em;"> Joan Didion was the clear winner though many of us are approaching it with some trepidation as we know it will be a very emotional read. Perhaps we'll make a field trip to see the </span><a href="http://www.magicalthinkingonbroadway.com/"><span style="font-size: 1.2em;">play</span></a><span style="font-size: 1.2em;">. Kudos to R. who hosted with excellent fare (thank goodness as it served as my dinner) and who served an amazing </span><a href="http://www.cnn.com/FOOD/news/9904/15/cookbook.awards/recipe1.html"><span style="font-size: 1.2em;">lemon olive oil cake</span></a><span style="font-size: 1.2em;">. It sounds weird, but is delicious.</span>

此致, 法鲁克·谢克。

这会将目标 <img>...</img> 标记捕获为匹配的第 1 组:

<a [^>]*>(<span [^>]*>)?(<img[^>]* \/>)(<\/span>)?<\/a>

demo

这里的技巧是使用 [^>]* 而不是 .*? 来仅使用标签的属性,从而正确匹配最近的父级。