我正在尝试匹配文字中的所有电话号码。
这个正则表达式适合我。当我检查正则表达式匹配网站时。但是当我在实际代码中使用时,它会给我错误的结果
>>> text = 'my phone is +31 478-511014 and +91 900 133 5555'
>>> mobile = re.findall(r'\+?(\d*)\s?\(?(\d*)\)?\s?(\d*)[\s-]?(\d*)\s?(\d*)\s?(\d*)\s?', text)
>>> mobile
[('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('31', '478', '', '511014', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('', '', '', '', '', ''), ('91', '900', '133', '5555', '', ''), ('', '', '', '', '', '')]
我做错了吗?
答案 0 :(得分:1)
如果您遇到问题,这个应该有用:
[(+\d]\d[-\d\s()]*\d
答案 1 :(得分:0)
mobile = re.findall(r' +?(\ d +)\ s?(?(\ d *))?\ s?(\ d *)[\ s - ]?(\ d *) \ s?(\ d *)\ s?(\ d *)\ s?',text)