Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex- how to stop at first occurrence of a character

Tags:

java

c#

regex

php

perl

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

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:

preg_match('/src=\"(.*)\"/', $row->find('a img',0), $matches);
if($matches){
   echo $matches[0];
}

prints out 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"

or if possible just... http://i.bookfinder.com/about/booksellers/logo_borderless/amazon_uk.gif

what should I be adding to the regex? Thanks

like image 370
mk_89 Avatar asked Nov 29 '22 14:11

mk_89


1 Answers

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=".

like image 190
Ωmega Avatar answered Dec 05 '22 07:12

Ωmega