Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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
Java 正则表达式-如何在字符首次出现时停止_Java_C#_Php_Regex_Perl - Fatal编程技术网

Java 正则表达式-如何在字符首次出现时停止

Java 正则表达式-如何在字符首次出现时停止,java,c#,php,regex,perl,Java,C#,Php,Regex,Perl,我试图从标记中提取src值, 到目前为止,我似乎能够提取src值和字符串中最后一个引号之间的字符串 字符串: <img border="0" src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt=""> preg_match('/src=\"(.*)\"/', $row->find('a img',0), $match

我试图从标记中提取src值, 到目前为止,我似乎能够提取src值和字符串中最后一个引号之间的字符串

字符串:

<img  border="0"  src="http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif" width="89" height="31" alt="">
preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches);
if($matches){
   echo $matches[0];
}
打印出来
src=”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“

或者,如果可能的话,只是…
http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif

我应该在正则表达式中添加什么?感谢您提供RegExp:

preg_match('/src="([^"]+)"/', $row->find('a img',0), $matches);
echo $matches[1];
如果我是对的,您正在使用简单的html\U dom\U解析器库。如果是这样,您只需键入:

$row->find('a img',0)->src

试试看,它应该适合你的需要

/src=\"[^\"]+\"/

你真的很接近>>

Yours:        preg_match('/src=\"(.*)\"/',  $row->find('a img',0), $matches);
Correct one:  preg_match('/src=\"(.*?)\"/', $row->find('a img',0), $matches);

通过添加
可以请求match
*
lazy,这意味着它将在需要时匹配任何内容,而不是在can之前匹配任何内容。如果没有lazy操作符,它将停在最后一个双引号
”前面,该双引号位于
alt=“

+1后面,用于
$row->find('a img',0)->src
,我希望这是可能的。请参阅下面的答案,以了解您在哪里犯了错误。/[^]+/工作更快,因为它很贪婪。您可以使用这个regexp,因为图像url不可能包含引号。@Serjio-Serjio,欢迎这样说。我非常擅长regex,我当然同意您关于性能的看法,因为如果可能的话,应该忽略懒惰运算符。这里的要点,我想通过我的回答来做的,是得到一些教训。正如你所看到的,我的答案是在
[^”]+
被建议之后发布的,所以我不想成为一个混蛋,在没有帮助的情况下发布相同的正则表达式。懒惰运算符的解释对于OP来说应该很重要。谢谢,我会记住这一点,我刚刚开始阅读正则表达式@Ω好吧,对不起。我不想激怒你。但是正如你所写的,现在每个人都会看到lazy和贪婪regexp之间的区别,并且知道我为什么使用贪婪的regexp。我不知道那个懒惰的操作符,只是帮我解了正则表达式。谢谢(+1):)