[英]How to extract phone numbers (numbers only) in Python using Regex from various formats from different countries?
我有来自不同国家的不同电话号码格式的列表,例如:
+1 999-888-7777
999-888-7777
(999)-888-7777
(999) 888-7777
(999) 888 7777
+444 111 222 3333
+99 1 88888888
0123456789
333-55555
+11111
999-888-7777 ex.7777
999.888.7777
我想让我的输出只返回实际的电话号码并删除任何类型的格式,比如
9998887777
9998887777
9998887777
9998887777
9998887777
1112223333
188888888
0123456789
33355555
11111
9998887777
9998887777
你能帮我用一个可以做到这一点的python中的正则表达式吗?
您可以使用re.findall
查找字符串中所有数字的实例并将结果列表连接在一起
''.join(re.findall(r'(\d+)', phone_number_str))
re.sub 在这里会很有用。
s = '''
+1 999-888-7777
999-888-7777
(999)-888-7777
(999) 888-7777
(999) 888 7777
+444 111 222 3333
+99 1 88888888
0123456789
333-55555
+11111
999-888-7777 ex.7777
999.888.7777
'''
print('\n'.join(re.sub('\D', '', x) for x in s.split('\n')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.