匹配手机号:
1 phone_str = "hey my name is alex, and my phone number is 13651054607, please call me if you are pretty!" 2 phone_str2 = "hey my name is alex, and my phone number is 18651054604, please call me if you are pretty!" 3 4 m = re.search("(1)([358]\d{9})",phone_str2) 5 if m: 6 print(m.group())
匹配IP V4:
1 ip_addr = "inet 192.168.60.223 netmask 0xffffff00 broadcast 192.168.60.255" 2 3 m = re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", ip_addr) 4 5 print(m.group())
分组匹配地址:
contactInfo = 'Oldboy School, Beijing Changping Shahe: 010-8343245' match = re.search(r'(\w+), (\w+): (\S+)', contactInfo) #分组 match = re.search(r'(?P<last>\w+), (?P<first>\w+): (?P<phone>\S+)', contactInfo)
匹配email:
email = "[email protected] http://www.oldboyedu.com" m = re.search(r"[0-9.a-z]{0,26}@[0-9.a-z]{0,20}.[0-9a-z]{0,8}", email) print(m.group())
contactInfo
=
'Oldboy School, Beijing Changping Shahe: 010-8343245'
match
=
re.search(r
'(\w+), (\w+): (\S+)'
, contactInfo)
#分组
"""
>>> match.group(1)
'Doe'
>>> match.group(2)
'John'
>>> match.group(3)
'555-1212'
"""
match
=
re.search(r
'(?P<last>\w+), (?P<first>\w+): (?P<phone>\S+)'
, contactInfo)