[英]regex with ? and { } quantifiers
I'm trying to create a regex to validate numbers that checks the year for leap year. 我正在尝试创建一个正则表达式来验证检查年份的数字是否为leap年。 here's a part of the code.
这是代码的一部分。 for some reason this code would let number 4 8 24 28 as a valid regex.
由于某种原因,此代码会将数字4 8 24 28用作有效的正则表达式。
(0{2}?)
([2468][480] | [13579][26])
pattern = re.compile (r"""
((0{2}?)([2468][480] | [13579][26]))
""", re.X)
when I left out 当我离开时
(0{2}?)
24 12 and everything works.. 24 12,一切正常。
I'm using verbose so spacing shouldn't matter.. 我使用的是详细信息,因此间距无关紧要。
Invalid 无效
12
24
28
16
EDIT :: Actually all is invalid now.. 编辑::实际上所有现在都是无效的。
i don't understand why 24 is invalid and 28 is invalid this doesn't make sense at all. 我不明白为什么24个无效和28个无效,这根本没有道理。 I appreciate your guidance.
感谢您的指导。
When you write (0{2}?)
, that means “match two 0
s here, but match as few as possible”. 当您写
(0{2}?)
时,表示“在此匹配两个0
,但要匹配的越少越好”。 Non-greediness doesn't really make sense for an {n}
quantifier (it does for {n,}
, and {m,n}
) – did you mean (0{2})?
不贪婪对
{n}
量词并没有任何意义(对{n,}
和{m,n}
确实如此)–您的意思是(0{2})?
? ?
Oh, and do keep in mind that years divisible by 400 are leap years. 哦,请记住,被400整除的年份是 leap年。
Using the re.DEBUG
flag to show debug info about the expression, we get 使用
re.DEBUG
标志显示有关表达式的调试信息,我们得到
>>> pattern = re.compile(r'0{2}?', re.DEBUG)
min_repeat 2 2
literal 48
The min_repeat
shows that 0{2}?
min_repeat
显示为0{2}?
isn't being interpreted as ?
不被解释为
?
applied to 0{2}
. 应用于
0{2}
。 It's being interpreted as a lazy quantifier, attempting to match 0 any number of times from 2 to 2, but as few as possible. 它被解释为惰性量词,尝试从2到2任意次数地匹配0,但要尽可能地少。 This doesn't quite seem consistent with the documentation;
这似乎与文档并不完全一致。 the docs only show the
{m,n}?
文档仅显示
{m,n}?
form. 形成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.