立即学习:https://edu.csdn.net/course/play/26755/340150?utm_source=blogtoedu
import re
print(re.match('.*hello', 'hello'))
s = "Today is 2020-01-23"
m = re.match('\d{4}-\d{2}-\d{2}', s)
# print(m.group())
# match匹配,search搜索
m = re.match('python.*', 'python language')
print(m)
m = re.search('python', 'python language')
print(m)
m = re.search('1\d{10}', '手机号是1222222222333333')
print(m)
# 123-12345678-1234
m = re.search('(\d{3})-(\d{7,})-(\d{3,})','电话是024-12345678-1234')
print(m.group())
print(m.groups())
print(m.groups()[0])
print(m.groups()[1])
print(m.groups()[2])
# findall
m = re.findall("[0-9a-zA-Z]+@+[0-9a-zA-Z]+\." + 'net|' + "[0-9a-zA-Z]+@+[0-9a-zA-Z]+\." + 'com', "我的Email是adf@163.com,或者sdaf@122.net,或者asdf@fdd.cn", re.I)
print(m)
def fun(matched):
return "<" + matched.group() + ">"
res = re.subn("-?\d+(\.\d+)?", fun, "PI is 3.1415926, 1 + 1.1 = 2.1")
print(res)