【re】模块运用,正则匹配

# re.search() 在一个字符串中搜索匹配正则表达式的第一个位置, 返回match对象
# re.match()  在一个字符串的开始位置起匹配正则表达式, 返回match对象
# re.findall()  搜索字符串, 以列表类型返回全部能匹配的子串
# re.split() 将一个字符串按照正则表达式匹配结果进行分割, 返回列表类型
# re.finditer() 搜索字符串, 返回一个匹配结果的迭代类型, 每个迭代元素是match对象
# re.sub()  在一个字符串中替换所有匹配正则表达式的子串, 返回替换后的字符串
# re.search(pattern, string, flags=0)
# 在一个字符串中搜索匹配正则表达式的第一个位置,返回match对象
# 1)pattern: 正则表达式的字符串或原生字符串表示
# 2)string: 待匹配字符串
# 3)flags: 正则表达式使用时的控制标记
# re.I(re.IGNORECASE) 忽略正则表达式的大小写,[A-Z]能够匹配小写字符
# re.M(re.MUTILINE)正则表达式中^操作符能够将给定字符串的每行当做匹配开始
# re.S(re.DOTALI)正则表达式中的操作符能够匹配所有字符,默认匹配除换行外的所有字符
import re
# search
match = re.search(r'[1-9]d{5}', 'BIT 100081')
if match:
    print(match.group(0))    #100081
# match
match2 = re.match(r'[1-9]d{5}', '100081 BIT')
if match2:
    print(match2.group(0))    #100081
# findall
ls = re.findall(r'[1-9]d{5}', '100081 BIT TSU100084')
print(ls)    #['100081', '100084']
# re.split(pattern, string,maxsplit=0,flags=0)
# 将一个字符串按照正则表达式匹配结果进行分割,返回列表类型
# pattern:正则表达式的字符串或原生字符串表示
# string:匹配字符串
# flags:正则表达式使用时的控制标记
split_list = re.split(r'[1-9]d{5}', 'BIT100081 TSU100084')
print(split_list)   #['BIT', ' TSU', '']
split_list2 = re.split(r'[1-9]d{5}', 'BIT100081 TSU100084', maxsplit=1)
print(split_list2)  #['BIT', ' TSU100084']
# re.finditer
for m in re.finditer(r'[1-9]d{5}', 'BIT100081 TSU100084'):
    if m:
        print(m.group(0))
# 100081
# 100084
# re.sub
# 在一个字符串中替换所有匹配正则表达式的子串,返回替换后的字符串
# 1)pattern: 正则表达式的字符串或原生字符串表示
# 2)repl: 替换匹配字符串的字符串
# 2)string: 待匹配字符串
# 3)count: 匹配的最大次数
# 4)flags: 正则表达式使用时的控制标记

s = re.sub(r'[1-9]d{5}', ':zipcode', 'BIT100081 TSU100084')
print('s----', s)   # s---- BIT:zipcode TSU:zipcode
# .group(0): 获得匹配后的字符串
# .start(): 匹配字符串在原始字符串的开始位置
# .end(): 匹配字符串在原始字符串的结束位置
# .span() 返回(.start(), .end())

m = re.search(r'[1-9]d{5}', 'BIT100081')
print(m.string)  # 匹配的字符串
print(m.re)  # 匹配使用的正则表达式
print(m.pos)  # 搜索字符串的开始位置
print(m.endpos)   # 搜索字符串的结束位置
print(m.start())  # 匹配结果在源字符串的 起始位置
print(m.end())   # 匹配结果在源字符串的 结束位置
print(m.span())  # 匹配结果在源字符串的 二连位置
print(m.group(0))
# Re库的贪婪匹配和最小匹配: 输出匹配最长的子串
# 最小匹配操作符:
# *?  前一个字符0次或无限次扩展, 最小匹配
# +?  前一个字符1次或者无限次扩展, 最小匹配
# ??  前一个字符0次或1次扩展, 最小匹配
# {m,n}? 扩展前一个字符m至n次(含n次), 最小匹配
#re.findall() - compile()与findall()一起使用,返回一个列表。
import re
#compile()函数返回的是一个匹配对象,它单独使用就没有任何意义,需要和findall(), search(), match()搭配使用。
content = 'Hello, I am Jerry, from Chongqing, a montain city, nice to meet you……'
regex = re.compile('w*ow*')
x = regex.findall(content)
print(x)    # ['Hello', 'from', 'Chongqing', 'montain', 'to', 'you']
#re.match() - compile()与match()一起使用,可返回一个class、str、tuple。
#但是一定需要注意match(),从位置0开始匹配,匹配不到会返回None,返回None的时候就没有span/group属性了,并且与group使用,返回一个单词'Hello'后匹配就会结束。
content = 'Hello, I am Jerry, from Chongqing, a montain city, nice to meet you……'
regex = re.compile('w*ow*')
y = regex.match(content)
print(y)    #<re.Match object; span=(0, 5), match='Hello'>
print(type(y))  #<class 're.Match'>
print(y.group())    #Hello
print(y.span()) #(0, 5)
#re.search() - compile()与search()搭配使用, 返回的类型与match()差不多
#但是不同的是search(), 可以不从位置0开始匹配。但是匹配一个单词之后,匹配和match()一样,匹配就会结束。
content = 'Hello, I am Jerry, from Chongqing, a montain city, nice to meet you……'
regex = re.compile('w*ow*')
z = regex.search(content)
print(z)  #<re.Match object; span=(0, 5), match='Hello'>
print(type(z))  #<class 're.Match'>
print(z.group())  #Hello
print(z.span())  #(0, 5)
原文地址:https://www.cnblogs.com/hsinfo/p/13682355.html