Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex extract字符串仅在span标记之间包含img标记_Regex_Preg Match_Preg Match All - Fatal编程技术网

Regex extract字符串仅在span标记之间包含img标记

Regex extract字符串仅在span标记之间包含img标记,regex,preg-match,preg-match-all,Regex,Preg Match,Preg Match All,我有要求,以获取只有在锚和跨度标签下的图像。 是否有人能提供获取图像的解决方案。 提前谢谢 详情: 示例代码链接: RegEx1:,莎拉·杜南特的,朱帕·拉希里的和朱莉·大冢的。琼·迪迪翁显然是赢家,尽管我们中的许多人都带着一些恐惧接近它,因为我们知道这将是一本非常感人的读物。也许我们会去实地考察一下。感谢R。他招待了我一顿丰盛的晚餐(谢天谢地,这顿饭是我的晚餐),他招待了我一顿美餐。听起来很奇怪,但很好吃。 问候,, Faruq Shaik。这将目标..标记捕获为匹配的第1组: <s

我有要求,以获取只有在锚和跨度标签下的图像。 是否有人能提供获取图像的解决方案。 提前谢谢

详情: 示例代码链接:

RegEx1:
,莎拉·杜南特的,朱帕·拉希里的和朱莉·大冢的。琼·迪迪翁显然是赢家,尽管我们中的许多人都带着一些恐惧接近它,因为我们知道这将是一本非常感人的读物。也许我们会去实地考察一下。感谢R。他招待了我一顿丰盛的晚餐(谢天谢地,这顿饭是我的晚餐),他招待了我一顿美餐。听起来很奇怪,但很好吃。
问候,,
Faruq Shaik。

这将目标
..
标记捕获为匹配的第1组:

 <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>


这里的诀窍是使用
[^>]*
而不是
*?
仅使用标记的属性,从而正确匹配最近的父项。

通常使用正则表达式解析HTML(或XML)会导致错误,因为SGML派生的标记语言不是正则的。但是,如果您的需求足够狭窄,并且输入HTML格式保守,您可能会侥幸逃脱。此外,请在问题中包含示例数据,以及您在标记中使用的正则表达式引擎。感谢您的快速响应,这是已放置示例代码的链接。我试过使用下面2个正则表达式代码,但没有成功。RegEx1:“(.*)”RegEx2:“”上的问题和答案不依赖于外部链接(尤其是基于flash的链接)。很抱歉,我已经用示例文本更新了我的帖子。谢谢这就是我要找的…太好了。谢谢你波西米亚人,这里,只有一些图片用span标签包装。但是我必须得到所有带有span标记或不带span标记的图像。我曾尝试在RegEx中编写一个条件(
(?(1)foo | bar)
),但它不起作用<代码>/]*>(?(]*>(]*\/>)]*>(]*\/>)|]*\/>)/
请帮助我在正则表达式中编写条件,因为我对正则表达式是全新的。Thanks@faruq我已经修改了正则表达式并更新了演示,以包含和示例,而不包含跨度。只要你的html格式足够好,这个新版本就可以工作。谢谢你的支持,我仍然无法编写正则表达式来获取图像,无论图像周围是否有span标记。我已经在你的演示中添加了额外的字符串,这两种场景都有。请帮助我获得输出@FaruqShaik链接UI未使用更新的正则表达式(我更改了它以满足您可选span的要求-请仔细查看我的答案的正则表达式并阅读前面的评论)。我已经更新了您发布的链接,以使用我当前的正则表达式-请参阅。看看是否已经有效。
<a [^>]*>(<span [^>]*>)?(<img[^>]* \/>)(<\/span>)?<\/a>