正则表达式 – 如何在第一次出现角色时停止
我试图从标签中提取src值,到目前为止我似乎能够提取字符串中src值和最终引号之间的字符串
串:
例如在PHP中:
preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches); if($matches){ echo $matches[0]; }
打印出 src="http://sofzh.miximages.com/java/amazon_uk.gif" width="89" height="31" alt=""
但我真正想要的是… src="http://sofzh.miximages.com/java/amazon_uk.gif"
或者如果可能的话…… http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif
我该怎么加入正则表达式? 谢谢
对于RegExp:
preg_match('/src="([^"]+)"/', $row->find('a img',0), $matches); echo $matches[1];
如果我是对的,你正在使用simple_html_dom_parser库。 如果这是真的你可以输入:
$row->find('a img',0)->src
你实际上非常接近>>
Yours: preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches); Correct one: preg_match('/src=\"(.*?)\"/', $row->find('a img',0), $matches);
加入?
你提出匹配请求.*
懒惰,这意味着它将匹配任何东西,直到需要,而不是任何东西,直到可以。 如果没有懒惰的运算符,它将停在最后一个双引号"
前面,这是在alt="
。
试试,它应该对你的需求有益
/src=\"[^\"]+\"/