公告
财富商城
积分规则
提问
发文
2018-12-31 19:20发布
春风洒进眼中
What is the difference between:
(.+?)
and
(.*?)
when I use it in my php preg_match regex?
preg_match
The first (+) is one or more characters. The second (*) is zero or more characters. Both are non-greedy (?) and match anything (.).
+
*
?
.
In RegEx, {i,f} means "between i to f matches". Let's take a look at the following examples:
{i,f}
i
f
{3,7}
{,10}
{3,}
{,}
{5}
Most good languages contain abbreviations, so does RegEx:
{1,}
{,1}
This means + requires at least 1 match while * accepts any number of matches or no matches at all and ? accepts no more than 1 match or zero matches.
Credit: Codecademy.com
They are called quantifiers.
* 0 or more of the preceding expression
+ 1 or more of the preceding expression
Per 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.*b will match "abab" (preg_match_all will return one match, the "abab")
a.*b
while a.*?b will match only the starting "ab" (preg_match_all will return two matches, "ab")
a.*?b
You can test your regexes online e.g. on Regexr, see the greedy example here
最多设置5个标签!
The first (
+
) is one or more characters. The second (*
) is zero or more characters. Both are non-greedy (?
) and match anything (.
).In RegEx,
{i,f}
means "betweeni
tof
matches". Let's take a look at the following examples:{3,7}
means between 3 to 7 matches{,10}
means up to 10 matches with no lower limit (i.e. the low limit is 0){3,}
means at least 3 matches with no upper limit (i.e. the high limit is infinity){,}
means no upper limit or lower limit for the number of matches (i.e. the lower limit is 0 and the upper limit is infinity){5}
means exactly 4Most good languages contain abbreviations, so does RegEx:
+
is the shorthand for{1,}
*
is the shorthand for{,}
?
is the shorthand for{,1}
This means
+
requires at least 1 match while*
accepts any number of matches or no matches at all and?
accepts no more than 1 match or zero matches.Credit: Codecademy.com
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.*b
will match "abab" (preg_match_all will return one match, the "abab")while
a.*?b
will 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