java regex-如何在第一次出现字符时停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11302690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
regex- how to stop at first occurrence of a character
提问by mk_89
I am trying to extract the src value from a tag, so far I seem to be able to extract the string between the src value and the final quotation mark in the string
我正在尝试从标签中提取 src 值,到目前为止我似乎能够提取 src 值和字符串中最后一个引号之间的字符串
String:
细绳:
<img border="0" src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt="">
e.g. in PHP:
例如在 PHP 中:
preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches);
if($matches){
echo $matches[0];
}
prints outsrc="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt=""
打印出来src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt=""
but what i really want printed is...src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif"
但我真正想要打印的是...src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif"
or if possible just...http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif
或者如果可能的话……http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif
what should I be adding to the regex? Thanks
我应该向正则表达式添加什么?谢谢
回答by ?mega
You were actually very close >>
你实际上非常接近>>
Yours: preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches);
Correct one: preg_match('/src=\"(.*?)\"/', $row->find('a img',0), $matches);
By adding ?
you make request for match .*
lazy, which means it will match anything until needed, not anything until can. Without lazy operator it will stop in front of last double-quote "
, which is behind alt="
.
通过添加?
你请求匹配.*
延迟,这意味着它会匹配任何东西直到需要,而不是任何东西直到可以。如果没有惰性运算符,它将停在最后一个双引号之前"
,后者在alt="
.
回答by Sergii Stotskyi
For RegExp:
对于正则表达式:
preg_match('/src="([^"]+)"/', $row->find('a img',0), $matches);
echo $matches[1];
If i'm right, you are working with simple_html_dom_parserlibrary. If that's true you can just type:
如果我是对的,那么您正在使用simple_html_dom_parser库。如果这是真的,你可以输入:
$row->find('a img',0)->src
回答by RolandasR
try, it should be good for your needs
试试吧,应该能满足你的需求
/src=\"[^\"]+\"/