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'?
{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)
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |