php 正则表达式加与星差?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8575281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex plus vs star difference?
提问by David19801
What is the difference between:
有什么区别:
(.+?)
and
和
(.*?)
when I use it in my php preg_match
regex?
当我在我的 phppreg_match
正则表达式中使用它时?
回答by stema
They are called quantifiers.
它们被称为量词。
*
0 or more of the preceding expression
*
0 个或多个前面的表达式
+
1 or more of the preceding expression
+
1 个或多个上述表达式
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.
在?
一个量词后改变行为,使这个量词“ungreedy”,意味着它会匹配尽可能少。
Example greedy/ungreedy
贪心/不贪心的例子
For example on the string "abab"
例如在字符串“ abab”上
a.*b
will match "abab" (preg_match_all will return one match, the "abab")
a.*b
将匹配“abab”(preg_match_all 将返回一个匹配项,即“abab”)
while a.*?b
will match only the starting "ab" (preg_match_all will return two matches, "ab")
whilea.*?b
将只匹配起始的“ab”(preg_match_all 将返回两个匹配项,“ab”)
You can test your regexes online e.g. on Regexr, see the greedy example here
您可以在线测试您的正则表达式,例如在 Regexr 上,请参阅此处的贪婪示例
回答by Quentin
The first (+
) is one or more characters. The second (*
) is zero or more characters. Both are non-greedy (?
) and match anything (.
).
第一个 ( +
) 是一个或多个字符。第二个 ( *
) 是零个或多个字符。两者都是非贪婪的 ( ?
) 并且匹配任何东西 ( .
)。
回答by DaveRandom
A +
matches one or moreinstances of the preceding pattern. A *
matches zero or moreinstances of the preceding pattern.
A+
匹配前面模式的一个或多个实例。A*
匹配前面模式的零个或多个实例。
So basically, if you use a +
there must be at least one instance of the pattern, if you use *
it will still match if there are no instances of it.
所以基本上,如果你使用 a+
必须至少有一个模式实例,如果你使用*
它,如果没有它的实例,它仍然会匹配。
回答by Xophmeister
+
matches at least one character
+
匹配至少一个字符
*
matches any number (including 0) of characters
*
匹配任意数量(包括 0)的字符
The ?
indicates a lazy expression, so it will match as few characters as possible.
该?
指示慵懒的表情,所以它会匹配尽可能少的字符越好。
回答by Azri Jamil
Consider below is the string to match.
考虑下面是要匹配的字符串。
ab
The pattern (ab.*)
will return a match for capture group with result of ab
该模式(ab.*)
将返回捕获组的匹配结果为ab
While the pattern (ab.+)
will not match and not returning anything.
虽然模式(ab.+)
将不匹配并且不返回任何内容。
But if you change the string to following, it will return aba
for pattern (ab.+)
但是如果将字符串更改为以下,它将返回aba
模式(ab.+)
aba
回答by jeroen
+
is minimal one, *
can be zero as well.
+
是最小的一,*
也可以为零。
回答by Madara's Ghost
A star is very similar to a plus, the only difference is that while the plus matches 1 or more of the preceeding character/group, the start matches 0 or more.
星号与加号非常相似,唯一的区别是加号匹配 1 个或多个前面的字符/组,而开头匹配 0 个或多个。
回答by Miladiouss
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}
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 4
{3,7}
意味着 3 到 7 场比赛{,10}
表示最多 10 场比赛,没有下限(即下限为 0){3,}
表示至少 3 个匹配,没有上限(即上限为无穷大){,}
表示匹配次数没有上限或下限(即下限为0,上限为无穷大){5}
正好是 4
Most good languages contain abbreviations, so does RegEx:
大多数优秀的语言都包含缩写,RegEx 也是如此:
+
is the shorthand for{1,}
*
is the shorthand for{,}
?
is the shorthand for{,1}
+
是简写{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.
这意味着+
需要至少 1 个匹配项,同时*
接受任意数量的匹配项或根本不?
接受匹配项,并且接受不超过 1 个匹配项或零个匹配项。
Credit: Codecademy.com
信用:Codecademy.com
回答by Crt
I think the previous answers fail to highlight a simple example:
我认为以前的答案未能突出一个简单的例子:
for example we have an array:
例如我们有一个数组:
numbers = [5, 15]
The following regex expression ^[0-9]+
matches: 15
only.
However, ^[0-9]*
matches both 5 and 15
. The difference is that the +
operator requires at least one duplicateof the preceding regex expression
以下正则表达式^[0-9]+
匹配:15
仅。但是,^[0-9]*
两者都匹配5 and 15
。不同之处在于+
运算符需要至少一个前面的正则表达式的副本