Python使用re模块提供了正则表达式处理能力
常量 | 说明 |
---|---|
re.M | 多行模型 |
re.S | 单行模型 |
re.l | 忽略大小写 |
re.X | 忽略表达式中的空白符 |
方法
import re
#元字符
# re.compile(pattern,flags=0)
# 设定flags 编译模式 返回正则表达式对象regex
# pattern就是正则表达式字符串 flags是选项 正则表达式需要被编译,为了提高效率,这些编译后的结果被保存,下次使用同样的pattern的时候,就不需要再次编译
# re的其他方法为了提高效率都调用了编译方法,就是为了提速
# 单次匹配
# re.match(pattern,string,flags)
# regex.match(string,[pos[,endpos]])
# match匹配从字符串的开头匹配,regex对象match方法可以重设开始位置和结束位置,返回match对象
# re.search(pattern,string,flags)
# regex.search(string,[pos[,endpos]])
# 从头搜索直到第一个匹配,regex对象search方法可以重设开始位置和结束位置,返回match对象
# re.fullmatch(pattern,string,flags)
# regex.fullmatch(string,[pos[,endpos]])
# 整个字符串和正则表达式匹配
s='''0123ac'''
# match 从头扫,找到匹配的立即返回对象
regex=re.compile('[ab]')
# matcher=re.match('\d',s) #每次调用需要编译
# print(type(matcher))
# print(1,matcher) # <_sre.SRE_Match object; span=(0, 1), match='0'>
# matcher=regex.match(s) #<_sre.SRE_Match object; span=(0, 1), match='0'>
# print(2,matcher)
# regex=re.compile('[ab]')
# matcher=regex.match(s) #<_sre.SRE_Match object; span=(0, 1), match='0'> 索引锁定为0
# print(3,matcher) #None 必须是从头
#
# matcher=regex.match(s,4) #<_sre.SRE_Match object; span=(0, 1), match='0'>
# print(4,matcher) #<_sre.SRE_Match object; span=(4, 5), match='a'>
# search 不指定从开头了,找到第一个匹配的立即返回对象
# matcher=re.search('[ab]',s)
# print(5,matcher)
#
# matcher=regex.search(s)
# print(6,matcher)
# 全匹配 全长匹配 全词匹配
matcher=re.fullmatch('[ab]',s)
print(7,matcher)
matcher=regex.fullmatch(s)
print(8,matcher)
# 7 None
# 8 None
matcher=re.fullmatch('\w+',s)
print(7,matcher) #<_sre.SRE_Match object; span=(0, 6), match='0123ac'>
matcher=regex.fullmatch(s,4,5)
print(8,matcher)
#全部匹配
# re.findall(pattern,string,flags)
# regex.findall(string,[pos[,endpos]])
# 对整个字符串 从左至右匹配 返回所有匹配项的列表
# re.finditer(pattern,string,flags)
# regex.finditer(string,[pos[,endpos]])
# 对整个字符串 从左至右匹配,返回所有匹配项,返回迭代器
# 注意每次迭代返回的是match对象
s='''0123abb'''
# regex=re.compile('[ab]')
# matcher=regex.findall(s)
# print(matcher) #['a', 'b']
# regex=re.compile('[ab]+')
# matcher=regex.findall(s)
# print(matcher) #['abb']
regex=re.compile('\D')
matcher=regex.findall(s)
print(matcher) #['a', 'b', 'b']
matcher=regex.finditer(s)
print(matcher) #<callable_iterator object at 0x7fca9497b160>
print(next(matcher))
# find_sub_split
# 匹配替换
# re.sub(pattern,replacement,string,count=0,flags=0)
# regex.sub(replacement,string,count=0)
# 使用pattern对字符串string进行匹配,对匹配项使用repl替换
#replace可以是string bytes function
# re.subn(pattern,replacement,string,count=0,flags=0)
# regex.subn(replacement,string,count=0)
# 同sub返回一个元组(new_string,number_of_subs_made)
# 分割字符串
# re.split(pattern,string,maxsplit=0,flags=0)
# re.split()分割字符串
import re
s='''01 bottle
02 bag
03 big1
100 able
'''
print(s.split())
result=re.split('[\s\d]+',s)
print(result)
regex=re.compile('^[\s\d]+')
result=regex.split(s)