【发布时间】:2012-09-21 10:03:16
【问题描述】:
下午好,
我正在尝试编写一个脚本,该脚本将通过其<img src=""/> 标签从文章中提取第一张图片。所以如果一篇文章有:
<p>Lorem ipsum dolor sit amet, labore et dolore magna aliqua.<img src="example.jpg"/> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
我想提取整个图片标签,<img src="example.jpg"/>.
我发现这个正则表达式只提取图像的位置:
content_to_extract_from[/img.*?src="(.*?)"/i,1]
产生,“example.jpg”。
有谁知道也可以捕获标签的正则表达式?
提前致谢,安迪
【问题讨论】:
-
your_html[ /\<img.*?src="(.*?)"\/\>/ ]这个怎么样? -
不要使用正则表达式,使用 nokogiri 之类的解析器
标签: html ruby-on-rails ruby regex image