- 论坛徽章:
- 0
|
我想匹配字符串以数字开头,以字母结尾,最少1个数字,最多3个数字,最少1个字母,最多2个字母。
比如:s='4j'或者ss = '345og'
>>> s='4j'
>>> ss = '345og'
>>> s
'4j'
>>> ss
'345og'
>>> re.search('^[0-9]{1,3}[a-zA-Z]{1,2}$',ss).group()
'345og'
>>> re.search('^[0-9]{1,3}[a-zA-Z]{1,2}$',s).group()
'4j'
但是,字符串s='456jk,56h,123ed'却不能匹配,如下所示:
>>> s='456jk,56h,123ed'
>>> re.search('^[0-9]{1,3}[a-zA-Z]{1,2}$',s).group()
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
re.search('^[0-9]{1,3}[a-zA-Z]{1,2}$',s).group()
AttributeError: 'NoneType' object has no attribute 'group'
>>> re.search('^[0-9]{1,3}[a-zA-Z]{1,2}$',s).group()
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
re.search('^[0-9]{1,3}[a-zA-Z]{1,2}$',s).group()
AttributeError: 'NoneType' object has no attribute 'group'
不知道问题出在哪里? |
|