正则_findall()方法

2018-08-20  本文已影响0人  测试媛617
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')]
上一篇下一篇

猜你喜欢

热点阅读