import re
phone = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phone.search('my phone is 415-111-2345')
print(mo.group(1))
print(mo.groups())
areaCode,mainNumber = mo.groups()
print(areaCode)
结果是:
415
('415', '111-2345')
415
import re
phone = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phone.search('my phone is 415-111-2345')
print(mo.group(1))
print(mo.groups())
areaCode,mainNumber = mo.groups()
print(areaCode)
结果是:
415
('415', '111-2345')
415
本文标题:正则_括号分组
本文链接:https://www.haomeiwen.com/subject/qogliftx.html
网友评论