美文网首页
正则_findall()方法

正则_findall()方法

作者: 测试媛617 | 来源:发表于2018-08-20 18:52 被阅读0次
    import re
    
    phone1 = re.compile(r'\d\d\d-\d\d\d\-\d\d\d\d')
    mo1 = phone1.search('cell:415-666-9876 work:567-857-8907')
    print(mo1.group())
    mo2 = phone1.findall('cell:415-666-9876 work:567-857-8907')
    print(mo2)
    
    # 调用有分组的正则表达式,findall()返回一个字符串的元组列表
    phone2 = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)')
    mo3 = phone2.findall('cell:415-666-9876 work:567-857-8907')
    print(mo3)
    
    结果是:
    415-666-9876
    ['415-666-9876', '567-857-8907']
    [('415', '666', '9876'), ('567', '857', '8907')]
    

    相关文章

      网友评论

          本文标题:正则_findall()方法

          本文链接:https://www.haomeiwen.com/subject/xvaliftx.html