3

Can someone tell me why the following does not match:

>>> re.search(r'(\d{2, 10})', '153')

and this one matches:

>>> re.search(r'\d{3}', '153')
<_sre.SRE_Match object at 0x02110368>

1 Answer 1

9

The re module does not like the space after the 2,:

In [2]: re.search(r'(\d{2, 10})', '153')

In [4]: re.search(r'(\d{2,10})', '153')
Out[4]: <_sre.SRE_Match object at 0x15c4648>

Once you have the space in there, the expression inside the braces is no longer recognized as the repetition operator. Instead, it becomes a literal match looking for {2, 10}:

In [11]: re.search(r'(\d{2, 10})', '1{2, 10}').group(0)
Out[11]: '1{2, 10}'
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.