这应该找到给定字符串中的所有电话号码
re.findall(r'+?(?[1-9][0-9 .-()]{8,}[0-9]', 来源)
>>> re.findall(r'[\+\(]?[1-9][0-9 .\-\(\)]{8,}[0-9]', Source)
['+60 (0)3 2723 7900', '+60 (0)3 2723 7900', '60 (0)4 255 9000', '+6 (03) 8924 8686', '+6 (03) 8924 8000', '60 (7) 268-6200', '+60 (7) 228-6202', '+601-4228-8055']
基本上,正则表达式列出了这些规则
使用
re
模块。
>>> import re
>>> Source = """<p><strong>Kuala Lumpur</strong><strong>:</strong> +60 (0)3 2723 7900</p>
<p><strong>Mutiara Damansara:</strong> +60 (0)3 2723 7900</p>
<p><strong>Penang:</strong> + 60 (0)4 255 9000</p>
<h2>Where we are </h2>
<strong> Call us on:</strong> +6 (03) 8924 8686
</p></div><div class="sys_two">
<h3 class="parentSchool">General enquiries</h3><p style="FONT-SIZE: 11px">
<strong> Call us on:</strong> +6 (03) 8924 8000
+ 60 (7) 268-6200 <br />
Fax:<br />
+60 (7) 228-6202<br />
Phone:</strong><strong style="color: #f00">+601-4228-8055</strong>"""
>>> for i in re.findall(r'\+[-()\s\d]+?(?=\s*[+<])', Source):
print i
+60 (0)3 2723 7900
+60 (0)3 2723 7900
+ 60 (0)4 255 9000
+6 (03) 8924 8686
+6 (03) 8924 8000
+ 60 (7) 268-6200
+60 (7) 228-6202
+601-4228-8055
>>>
我使用下面的正则表达式从字符串中提取手机号码。
import re
sent="this is my mobile number 9999922118"
phone = re.search(r'\b[789]\d{9}\b', sent, flags=0)
if phone:
phone.group(0)
模式 = "(+)?([0-9]{1,3})?( )?(([0-9]{1,3}))?( )?[(\d+((- \d+)+)]{10,15}"
import re
sent = "Tampa, FL 33602 PH: 813-202-7100 FAX: 813-221-8837 phone +60 (0)3 2723 7900"
pattern = "(\+)?([0-9]{1,3})?( )?(\([0-9]{1,3}\))?( )?[(\d+((\-\d+)+)]{10,15}"
phone = re.findall(r'{}'.format(pattern), sent, flag=0)
这应该找到字符串中的所有电话号码。