Thursday 19 October 2017

php - Differences between (.*) and (.*?)






Possible
Duplicate:

href="https://stackoverflow.com/questions/2301285/what-does-lazy-and-greedy-means-in-regexp">what
does lazy and greedy means in regexp?






I
know that in Regex the question mark after *, + or ? means ungreedy but if I want to
match any character, what is the difference between using (.*) or (.*?)
?




Thanks.



EDIT:
In
my case I want to check a URL. What are the differences
between



http://site\.net/(.*?)\.html


and




http://site\.net/(.*)\.html


?



Answer




Assume that you got this
url:



http://example.net/some/wierd/path.html?returnTo=somedoc.html



Greedy
would match entire
line:



http://example.net/some/wierd/path.html?returnTo=somedoc.html


while
non greedy
returns:



http://example.net/some/wierd/path.html



href="http://refiddle.com/" rel="nofollow noreferrer">http://refiddle.com/
is great for trying out regular expressions



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...