作者:奋斗的小鸟 | 来源:互联网 | 2022-12-27 14:02
这是我到目前为止:
import re
text = "If you want to call me, my old number was 905-343-2112 and it has
been changed to 289-544-2345"
phOne= re.findall(r'((\d{3})-(\d{3})-(\d{4}))', text)
for call in phone:
print (call[0])
我猜我的正则表达式找不到电话号码是不是很好,因为如果我在打印电话时取出方括号,它似乎给了我整数,然后它分解了每组数字.我怎么能抛光这段代码
1> iBug..:
将非捕获组用于电话号码的片段:
phOne= re.findall(r'((?:\d{3})-(?:\d{3})-(?:\d{4}))', text)
^^ ^^ ^^
或者更好的是,只需删除括号即可
phOne= re.findall(r'\d{3}-\d{3}-\d{4}', text)