RegEx Multiple Matches -
i'm having trouble regex match. here string:
(<a href="http://www.test.com/test/test.jpg">lorem ipsum dolor sit amet, consectetur adipiscing elit.</a>) lorem ipsum dolor <a href="http://www.test.com/test/test.jpg">sit amet</a> consectetur adipiscing elit.
the regex pattern i'm using is:
/(<)(.*=")(.*)(">)(.*)(<\/.*>)/g
the problem it's picking 1 match because of .*
before in last matching group in regex pattern. want find 2 matches of pattern (which there in string). how stop @ first instance of >
when searching? figure trick.
i've heard called 'non-greedy'? i've tried +
, ?
neither seem work i'm doing.
thanks!
- fyi , fwiw, accepted wisdom on regex not best way parse html...
- but if you're sticking regex, main problem
.*
quantifiers eat characters end of string. can fixed adding?
make quantifiers "lazy":.*?
the *
quantifier means zero or more
. causes .
dot match every single character end of string... then, allow rest of regex match, engine backtracks... .*
ends matching longest match, not shortest one. in contrast, .*?
on road shortest match (with caveats explained in articles below.)
reference
Comments
Post a Comment