2 回答

TA贡献1831条经验 获得超4个赞
我建议re.findall在这里使用:
inp = "The number is +31 713176319 and 650068168 is another one."
matches = re.findall(r'(?:^|(?<!\S)(?!\+\d+)\S+ )(\d{9})\b', inp)
print(matches)
这打印:
['650068168']
这里的正则表达式策略是匹配 9 位独立数字,当它出现在字符串的最开头时,或者它前面有一些不是国家/地区代码前缀的“单词”(此处松散定义的单词)\S+。
这是所使用的正则表达式的解释:
(?:
^ from the start of the string
| OR
(?<!\S) assert that what precedes is whitespace or start of the string
(?!\+\d+) assert that what follows is NOT a country code prefix
\S+ match the non prefix "word", followed by a space
)
(\d{9}) match and capture the 9 digit number
\b word boundary
添加回答
举报