正则表达式从5开始

ora*_*ife 3 python regex

通过使用正则表达式函数寻求帮助,该函数查找以5开头且长度为7位数字的字符串。

这是我到目前为止根据搜索得出的结果,但不起作用:

import re

string = "234324, 5604020, 45309, 45, 55, 5102903"
re.findall(r'^5[0-9]\d{5}', string)
Run Code Online (Sandbox Code Playgroud)

不知道我在想什么。

谢谢

use*_*203 5

您正在使用^,它在字符串的开头断言位置。请改用单词边界。另外,您既不需要[0-9]和也需要\d

使用\b5[0-9]{6}\b(或\b5\d{6}\b)代替:

>>> re.findall(r'\b5\d{6}\b', s)
['5604020', '5102903']
Run Code Online (Sandbox Code Playgroud)