python--------re大法(正则表达式)

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'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值