正则表达式没有得到预期结果
import restr = "123456789123456789 123456789123456"
# matchObj = re.findall(r'\d{18}|\d{15}', str)
matchObj = re.findall(r'\d{15}(\d{3})?', str)
print('str is: ', str)
print('matchObj is: ', matchObj)
\d{15}(\d{3})?我觉得应该至少有15个数字,问号代表有或无,整个式子匹配15或18个数字。
但结果如图所示,只匹配了三个数字
str is:123456789123456789 123456789123456
matchObj is:['789', ''] >>> import re
>>> s="123456789987654321"
>>> re.findall("\d{15}")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: findall() missing 1 required positional argument: 'string'
>>> re.findall("\d{15}",s)
['123456789987654']
>>> re.findall("\d{18}",s)
['123456789987654321']
>>>
没啥问题啊
>>> s="123456789987654321DFG123456789876543"
>>> re.findall("\d{18}|d{15}",s)
['123456789987654321']
>>> re.findall("\d{18}|\d{15}",s)
['123456789987654321', '123456789876543']
>>>
wp231957 发表于 2022-6-27 13:23
没啥问题啊
>>> s="123456789987654321DFG123456789876543"
matchObj = re.findall(r'\d{15}(\d{3})?', str)
是这个 李万金 发表于 2022-6-27 14:11
matchObj = re.findall(r'\d{15}(\d{3})?', str)
是这个
matchObj = re.findall(r'(\d{15}\d{3})?', str) nikezhi 发表于 2022-6-27 14:23
matchObj = re.findall(r'(\d{15}\d{3})?', str)
>>> s
'123456789987654321DFG123456789876543'
>>> re.findall("\d{15}\d?\d?\d?",s)
['123456789987654321', '123456789876543']
>>>
页:
[1]