量词如何运作?

ove*_*nge 2 python regex python-3.x python-3.6

>>>
>>> re.search(r'^\d{3, 5}$', '90210')  # {3, 5} 3 or 4 or 5 times
>>> re.search(r'^\d{3, 5}$', '902101')  # {3, 5} 3 or 4 or 5 times
>>> re.search(r'^\w{3, 5}$', 'hello')  # {3, 5} 3 or 4 or 5 times
>>> re.search(r'^\w{3, 5}$', 'hell')  # {3, 5} 3 or 4 or 5 times
>>>
Run Code Online (Sandbox Code Playgroud)

所有上述假设都应该有效,{}量词


题:

为什么r'^\d{3, 5}$'不搜索'90210'

fal*_*tru 6

{m,和之间应该没有空格n}:

>>> re.search(r'^\d{3, 5}$', '90210')  # with space


>>> re.search(r'^\d{3,5}$', '90210')  # without space
<_sre.SRE_Match object at 0x7fb9d6ba16b0>
>>> re.search(r'^\d{3,5}$', '90210').group()
'90210'
Run Code Online (Sandbox Code Playgroud)

BTW,902101与模式不匹配,因为它有6位数:

>>> re.search(r'^\d{3,5}$', '902101')
Run Code Online (Sandbox Code Playgroud)