Reputation: 11
As far as I know, * means in regex match more than 0 occurrence. And {n,m} means equal or more than n occurrence and equal or less than m occurrence.
But the below code is something strange. What I want to do is find numbers.
0 >>> import re
1 >>>
2 >>> re.search(r'[a-z]{4,6}[0-9]{2,6}', 'bird99')
3 <re.Match object; span=(0, 6), match='bird99'>
4 >>> re.search(r'[0-9]{2,6}', 'bird99')
5 <re.Match object; span=(4, 6), match='99'>
6 >>> re.search(r'[0-9]{0,6}', 'bird99')
7 <re.Match object; span=(0, 0), match=''>
8 >>> re.search(r'[0-9]*', 'bird99')
9 <re.Match object; span=(0, 0), match=''>
I expect match for 99 on result line number 7 and 9, but it doesn't make any match. Anybody can explain this result?
Thanks in advance.
Upvotes: 0
Views: 151
Reputation: 55619
r[0-9]*
matches zero or more ASCII digits. re.search
finds the first match, which is zero digits. Compare the result for re.findall
:
>>> re.search(r'[0-9]*', 'bird99')
<re.Match object; span=(0, 0), match=''>
>>> re.findall(r'[0-9]*', 'bird99')
['', '', '', '', '99', '']
Upvotes: 1