`regex{n,}?` == `regex{n}`? [英] `regex{n,}?` == `regex{n}`?
问题描述
-edit- 注意 末尾的
?
.{2,}?
-edit- NOTE the ?
at the end of .{2,}?
我发现你可以写
.{2,}?
这不是和下面一模一样吗?
Isnt that exactly the same as below?
.{2}
推荐答案
不,它们是不同的.^.{2,}?$
匹配长度至少 2 (如在 rubular.com 上看到的):
No, they are different. ^.{2,}?$
matches strings whose length is at least 2 (as seen on rubular.com):
12
123
1234
相比之下,^.{2}$
只匹配长度恰好 2 (如在 rubular.com 上看到的).
By contrast, ^.{2}$
only matches strings whose length is exactly 2 (as seen on rubular.com).
不情愿是正确的,.{2,}?
将首先尝试仅匹配两个字符.但是要使整体模式匹配,可能需要更多. 不是这种情况.{2}
只能匹配 2 个字符.
It's correct that being reluctant, .{2,}?
will first attempt to match only two characters. But for the overall pattern to match, it can take more. This is not the case with .{2}
, which can only match exactly 2 characters.
- Difference between
.*?
and.*
for regex
这篇关于`regex{n,}?` == `regex{n}`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!