regex- how to stop at first occurrence of a character

10,824

Solution 1

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_parser library. If that's true you can just type:

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

Solution 2

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

Solution 3

try, it should be good for your needs

/src=\"[^\"]+\"/
Share:
10,824
mk_89
Author by

mk_89

Updated on July 07, 2022

Comments

  • mk_89
    mk_89 almost 2 years

    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

  • mk_89
    mk_89 almost 12 years
    +1 for the $row->find('a img',0)->src, I was hoping this would be possible.
  • Sergii Stotskyi
    Sergii Stotskyi almost 12 years
    /[^"]+/ works faster because it's greedy. You can use this one regexp because it's impossible that image url will contain quote.
  • Ωmega
    Ωmega almost 12 years
    @Serjio - Serjio, welcome on SO. I am VERY GOOD in regex and I certainly agree with you about performance, as lazy operator shold be omit if possible. The point here, what I wanted to do by my answer, is to get OP some lesson. As you can see, my answer was posted after [^"]+ has been suggested, so I didn't want to be a jerk and post same regex with no help. Explanation of lazy operator should be important to learn for OP.
  • mk_89
    mk_89 almost 12 years
    Thanks I'll keep that in mind, I've just started reading up on regex
  • Sergii Stotskyi
    Sergii Stotskyi almost 12 years
    @Ωmega ok i'm sorry. I didn't want to provoke you. But as you wrote, now everybody will see the difference between lazy and greedy regexp and will know why i have used greedy one.
  • John
    John about 10 years
    Nice to compare the two regex, makes finding a difference easier. didn't know about the lazy operator, just helped me solve my regex. thanks(+1) :)