Python的正则表达式
Python使用re模块提供了正则表达式处理的能力
常量
常量 | 说明 |
---|---|
re.M | re.MULTILINE | 多行模式 |
re.S | re.DOTALL | 单行模式 |
re.l | re.IGNORECASE | 忽略大小写 |
re.X | re.VERBOSE | 忽略表达式中的空白字符 |
使用 | 位或 运算开启多种选项 |
方法
- 编译
re.compile(pattern,flags=0)
设定flags,编译模式,返回正则表达式对象regex.
pattern就是正则表达式字符串,flags是选项.正则表达式需要被编译,为了提高效率,这些编译后的结果被保存,下次使用同样的pattern的时候,就不需要再次编译.
re的其他方法为了提高效率都调用了编译方法,就是为了提速 - 单次匹配
re.mach(pattern,string,flags=0)
regex.match(string[, pos[, endpos]])
match匹配从字符串的开头匹配,regex对象match方法可以重设定开始位置和结束位置,返回match对象
re.search(pattern,string,flags=0)
regex.search(string[, pos[, endpos]])
从头搜索直到第一个匹配,regex对象search方法可以设定开始位置和结束位置,返回match对象
re.fullmatch(pattern,string,flags=0)
regex.fullmatch(string[, pos[, endpos]])
整个字符串和正则表达式匹配
import re
s = "bootle\nbag\nbig\napple"
for i,c in enumerate(s,1):
print((i-1,c),end= '\n' if i%10==0 else ' ')
# match方法
result = re.match('b',s)
print(result)
result = re.match('a',s)
print(result)
result = re.match('^a',s,re.M)
print(result)
result = re.match('^a',s,re.S)
print(result)
regex = re.compile('a')
result = regex.match(s)
print(5,result)
result = regex.match(s,15)
print(6,result)
# search方法
result = re.search('a',s)
print(result)
regex = re.compile('b')
result = regex.search(s,1)
print(result)
regex = re.compile('^b',re.M)
result = regex.search(s)
print(result)
result = regex.search(s,8)
print(result)
# fullmatch方法
result = re.fullmatch('bag', s)
print(result)
regex = re.compile('bag')
result = regex.fullmatch(s)
print(result)
result = regex.fullmatch(s, 7)
print(result)
result = regex.fullmatch(s, 7, 10)
print(result)
- 全文搜索
re.findall(pattern,string,flags=0)
regex.findall(string[, pos[, endpos]])
对整个字符串,从左至6右匹配,返回所有匹配项的列表
re.finditer(pattern,string,flags=0)
regex.finditer(string[, pos[, endpos]])
对整个字符串,从左至右匹配,返回所有匹配项,返回迭代器.
注意每次迭代返回的是match对象
import re
s = "bootle\nbag\nbig\napple"
for i,c in enumerate(s,1):
print((i-1,c),end= '\n' if i%10==0 else ' ')
# findall方法
result = re.findall('b',s)
print(1,result)
regex = re.compile('^b')
result = regex.findall(s)
print(result)
regex = re.compile('^b',re.M)
result = regex.findall(s,7)
print(result)
regex = re.compile('^b',re.S)
result = regex.findall(s)
print(result)
regex = re.compile('^b',re.M)
result = regex.findall(s,7,10)
# finditer方法
result = regex.finditer(s)
print(type(result))
r = next(result)
print(r.start(),r.end(),s[r.start():r.end()])
r = next(result)
print(type(r),r)
print(r.start(),r.end(),s[r.start():r.end()])
- 匹配替换
re.sub(pattern,replacement,string,count=0,flags=0)
regex.sub(replacemrnt,string,count=0)
使用pattern对字符串string进行匹配,对匹配项使用repl替换.
replacement可以是string,bytes,functions.
re.subn(pattern,replacement,string,count=0,flags=0)
regex.subn(replacement,string,count=0)
同sub返回一个元组(new_string,number_of_subs_made)
import re
regex = re.compile('b\wg')
result = regex.sub('magedu',s)
print(result)
result = regex.sub('magedu',s,1)
print(result)
regex = re.compile('\s+')
result = regex.subn('\t',s)
print(result)
- 分割字符串
字符串分割函数split,太难用,不能指定多个字符进行分割
re.split(pattern,sreing,maxsplit = 0,flags = 0)
re.split分割字符串
import re
s = """
os.path.abspath(path)
normpath(join(os.getcwd(),path)).
"""
print(re.split('\W',s))
regex = re.compile('\W')
result = regex.split(s)
print(result)
- 分组
使用小括号的pattern捕获的数据被放在组group中
match,seach函数可以返回match对象;findall返回字符串列表;finditer返回一个个match对象
如果pattern中使用了分组,如果有匹配的结果,会在match对象中
1, 使用group(N)方式返回对相应分组,1到N时对应的分组,0返回整个匹配的字符串,N不写缺省为0
2, 如果使用了命名分组,可以使用group(‘name’)的方式取分组
3, 也可以使用groups()返回所有组
4, 使用groupdict()返回所有命名的分组
# 分组
s = "bootle\nbag\nbig\napple"
regex = re.compile('(b\w+)')
result = regex.match(s)
print(type(result))
print(1,'match',result.groups())
result = regex.search(s,2)
print(2,'seach',result.groups())
# 命名分组
s = "bootle\nbag\nbig\napple"
regex= re.compile('(b\w+)\n(?P<name2>b\w+\n(?P<name3>b\w+))')
result = regex.match(s)
print(3,'match',result)
print(4,result.group(3),result.group(2),result.group(1))
print(5,result.group(0).encode())
print(6,result.group('name2'),result.group('name3'))
print(7,result.groups())
print(8,result.groupdict())
result = regex.findall(s)
for x in result:
print(type(x),x)
regex = re.compile('(?P<head>b\w+)')
result = regex.finditer(s)
for x in result:
print(type(x),x,x.group(),x.group('head'))