text1 = 'three digits NUMBER_001'
以下代码在字符" NUMBER _":['NUMBER_123']
之后的字符串中正确找到三位数:
result = re.findall('NUMBER_\d\d\d', text1)
print result
但如果数字超过三位数,则返回不完整的数字:
text2 = 'four digits number is NUMBER_0001'
result = re.findall('NUMBER_\d\d\d', text2)
有没有办法找到字符后面的所有连续数字" NUMBER _"?
答案 0 :(得分:1)
只需在您的注册表中使用()
:
result = re.findall('NUMBER_(\d\d\d)', text1)
同样应写为,
result = re.findall('NUMBER_(\d{3})', text1)
或者,任意数量的数字:
result = re.findall('NUMBER_([\d]+)', text1)
OR
result = re.findall('NUMBER_([0-9]+)', text1)
答案 1 :(得分:0)
只需将你的正则表达式替换为:
result = re.findall('NUMBER_([0-9]+)', text1)