RegEx废话电话号码

时间:2017-05-28 16:00:02

标签: python regex python-3.x

我有这个程序(Python):

import re

line = "This is a phone number: (061) - 535555. And this is another: 01 - 4657289, 9846012345"

foundNum = re.search(r'((\(?\d{2,3}\)?\D{0,3}\d{6,10})|\d{10})', line)

print("The phone numbers found are: ", foundNum.groups())

我试图从'line'字符串中提取所有电话号码(可能是另一个文本块),但它没有给出预期的结果,只有一个值(第一个电话号码)重复两次。我错过了什么?

3 个答案:

答案 0 :(得分:1)

您应该使用finditer

import re

line = "This is a phone number: (061) - 535555. And this is another: 01 - 4657289, 9846012345"

foundNum = re.finditer(r'((\(?\d{2,3}\)?\D{0,3}\d{6,10})|\d{10})', line)
for i in foundNum:
    print(i.group())

findall

import re

line = "This is a phone number: (061) - 535555. And this is another: 01 - 4657289, 9846012345"

foundNum = re.findall(r'((\(?\d{2,3}\)?\D{0,3}\d{6,10})|\d{10})', line)
print("The phone numbers found are: ", *foundNum)

答案 1 :(得分:1)

您可以使用findall代替search,也可以取消嵌套捕获组:

foundNum = re.findall(r'(\(?\d{2,3}\)?\D{0,3}\d{6,10}|\d{10})', line)
print("The phone numbers found are: ", foundNum)

<强>结果

('The phone numbers found are: ', ['(061) - 535555', '01 - 4657289', '9846012345'])

re.search()在字符串中搜索 第一次 出现的RE模式,这就是为什么它只返回第一个匹配。表达式re.findall()将字符串中模式的所有非重叠匹配作为字符串列表返回。

答案 2 :(得分:1)

您可以使用(demo on regex101.com):

\(?\d[- \d()]*\d

细分说:

\(?       # match ( eventually
\d        # match a digit
[- \d()]* # one of "-", "0-9", "(", ")" or " ", zero or more times
\d        # another digit

这确保了开头和结尾至少有一个数字。