What is the difference between:
(.+?)
and
(.*?)
when I use it in my php preg_match regex?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They are called quantifiers.
*0 or more of the preceding expression+1 or more of the preceding expressionPer default a quantifier is greedy, that means it matches as many characters as possible.
The
?after a quantifier changes the behaviour to make this quantifier “ungreedy”, means it will match as little as possible.Example greedy/ungreedy
For example on the string “abab“
a.*bwill match “abab” (preg_match_all will return one match, the “abab”)while
a.*?bwill match only the starting “ab” (preg_match_all will return two matches, “ab”)You can test your regexes online e.g. on Regexr, see the greedy example here