我要求只获取锚点和span标签下的图像。 有人可以提供获取图像的解决方案。 提前谢谢。
细节: 示例代码链接:http://regexr.com/v1?38u5g
RegEx1:<a (.*?)><span (.*?)>(.*?)<\/span><\/a>
RegEx2:<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&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&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>
此致 Faruq Shaik。
答案 0 :(得分:1)
这会将目标<img>...</img>
标记捕获为匹配的第1组:
<a [^>]*>(<span [^>]*>)?(<img[^>]* \/>)(<\/span>)?<\/a>
请参阅demo。
这里的诀窍是使用[^>]*
而不是.*?
来仅使用标记的属性,从而正确匹配最近的父级。