import re
将正则表达式编译成一个pattern对象
pattern = re.compile('\d')
pattern.match(): # 从起始位置开始匹配,往后查找.返回第一个符合规则的值
pattern.search(): # 从任何位置开始匹配,往后查查,返回第一个符合规则的值
pattern.findall(): # 所有的全部匹配,返回列表
pattern.finditer(): # 所有的全部匹配,返回的是一个迭代器
pattern.split(): # 分割字符串,返回列表
pattern.sub(): # 替换
# match (str,begin,end)
pattern = re.compile('\d+')
string = '12345asd23gfd'
m = pattern.match(string)
m.group()
'12345'
pattern = re.compile(r"([a-z]+) ([a-z]+)",re.I) # re.I 表示忽略大小写. re.S 表示全文匹配
string = 'hello World hello pattern'
m = pattern.match(string)
m.group(2)
'World'
m.span(1) #返回索引
(0, 5)
pattern = re.compile(r'\d+',re.I)
string = '1a2b3c4d'
m = pattern.search(string)
m.group()
m.span()
(0, 1)
import re
pattern = re.compile(r"\d+",re.S) #匹配1次或者多次
string = '1n22nn3n4n5nm6m3'
m = pattern.findall(string)
m
['1', '22', '3', '4', '5', '6', '3']
pattern = re.compile(r"\d?",re.S) # ?匹配0次或者一次
string = '1n22nn3n4n5nm6m3'
m = pattern.findall(string)
m
['1', '', '2', '2', '', '', '3', '', '4', '', '5', '', '', '6', '', '3', '']
pattern = re.compile(r"\d*",re.S) # 匹配任意次数
string = '1n22nn3n4n5nm6m3'
m = pattern.findall(string)
m
['1', '', '22', '', '', '3', '', '4', '', '5', '', '', '6', '', '3', '']
import re
string = r'''
Accept:text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
'''
pattern = re.compile('(.*):(.*)')
# pattern.findall(string)
pattern.sub(r'"\1":"\2"',string) # 替换的规则
'\n"Accept":"text/html,application/xhtml+xm\xe2\x80\xa6plication/xml;q=0.9,*/*;q=0.8"\n"Accept-Encoding":"gzip, deflate, br"\n"Accept-Language":"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"\n'