正则_findall()方法

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()方法)