【问题标题】:Python regex to extract phone numbers from stringPython正则表达式从字符串中提取电话号码
【发布时间】:2016-05-23 14:17:03
【问题描述】:

我对正则表达式非常陌生,使用 python 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>&nbsp;Call us on:</strong>&nbsp;+6 (03) 8924 8686
        </p></div><div class="sys_two">
    <h3 class="parentSchool">General enquiries</h3><p style="FONT-SIZE: 11px">
     <strong>&nbsp;Call us on:</strong>&nbsp;+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>"""

所以当我编译模式时,我应该能够找到使用

phone = re.findall(pattern,source,re.DOTALL)

 ['+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']

请帮我找出正确的模式

【问题讨论】:

    标签: regex python-2.7 python-3.x pattern-matching


    【解决方案1】:

    这应该找到给定字符串中的所有电话号码

    re.findall(r'+?(?[1-9][0-9 .-()]{8,}[0-9]', Source)

     >>> 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']
    

    基本上,正则表达式列出了这些规则

    1. 匹配的字符串可能以 + 或 ( 符号开头
    2. 后面必须跟一个 1-9 之间的数字
    3. 必须以 0-9 之间的数字结尾
    4. 中间可能包含 0-9(空格).-()。

    【讨论】:

    • 太棒了,这也适用于 golang,因为它似乎坚持相同的基本兼容的正则表达式
    【解决方案2】:

    使用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>&nbsp;Call us on:</strong>&nbsp;+6 (03) 8924 8686
            </p></div><div class="sys_two">
        <h3 class="parentSchool">General enquiries</h3><p style="FONT-SIZE: 11px">
         <strong>&nbsp;Call us on:</strong>&nbsp;+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
    >>> 
    

    【讨论】:

    • 我如何特别避免使用小于 10 个字符的列表元素或包含使用 re 的字母。
    • 谢谢阿维纳什。经过长时间的尝试,这个正则表达式只能完美运行。
    【解决方案3】:

    我使用下面的正则表达式从字符串中提取手机号码。

    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)
    

    【讨论】:

      【解决方案4】:

      pattern = "(+)?([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)
      

      这应该会找到字符串中的所有电话号码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-20
        相关资源
        最近更新 更多