正则表达式讲解(详细备注)

import re
res = re.match("^lei\d+","lei123 wodeshebao") #
print("输出结果为1:",res.group()) #输出结果为: lei123
# . 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
res1 = re.match(".","\nlei123 wodeshebao",flags=re.DOTALL)
print("输出结果为2:",res1.group()) #输出结果为:
res2 = re.match(".","lei123 wodeshebao")
print("输出结果为3:",res2.group())

输出结果为:

正则表达式讲解(详细备注)_第1张图片

# ^ 匹配字符串开头,若指定flags MULTILINE,这种也可以匹配上
res3 = re.match("^ab","abc\nleizi",flags=re.MULTILINE)
print(res3.group()) #ab
# $ 匹配字符串结尾
res4 = re.search("wo$","Iwo\nsadif",flags=re.MULTILINE).group()
print(res4) #wo
# * 匹配*号前的字符串0次或多次
res5 = re.findall("abc*","abcccsd ab9ji abc4dghf")
print(res5) #['abccc', 'ab', 'abc']
# + 匹配前一个字符1次或多次
res6 = re.findall("ab+","abbhiiog abjii ajf")
print(res6) #['abb', 'ab']
# ? 匹配前一个字符1次或0次
res7 = re.findall("ab?","ajjf abjjk abbc")
print(res7) #['a', 'ab', 'ab']
# {m} 匹配前一个字符m次
res8 = re.search(r"ab{3}","abbbm ababbbnn abcm am").group()
print(res8) #abbb
# (...) 分组匹配
res9 = re.search("(abc){2}ad(12|34)c","abcabcad34c").group()
print(res9) #abcabcad34c
# \A 只从字符串开头匹配
str1 = re.search("\Aabc","abclexabc")
print(str1) #
# \z 匹配字符串结尾,同$
str2 = re.search("ab$","abvfggab").group()
print(str2) #ab
# \d 匹配数字0-9
str3 = re.search("\d","wefftg3kkgh2")
print(str3) #
# \D 匹配非数字
str4 = re.search("\D+","ashj12234fd2345").group()
print(str4) #ashj
# \w 匹配[A-Za-z0-9]
str5 = re.search("\w+","D98asji&@jgif87").group()
print(str5) #D98asji
# \W 匹配非[A-Za-z0-9]
str6 = re.search("\W+","D98asji&@jgif87").group()
print(str6) #&@
# s 匹配空白字符、\t,\r
str7 = re.search("\s+","ab\tdd\noi").group()
print(str7,"前面是匹配结果") #	 前面是匹配结果

re–groupdict用法、split用法、sub替换用法

#第一种写法
result = re.search("(?P[0-9]+)(?P[a-zA-Z]+)","abc123leiyu@12345").groupdict()
print(result) #{'id': '123', 'name': 'l'}
print(result["id"]) #123
#第二种写法
result1 = re.search("(?P[0-9]+)(?P[a-zA-Z]+)","abc123leiyu@12345").group("name")
print(result1) #leiyu
#匹配身份证中的省市县和生日
result2 = re.search("(?P[0-9]{3})(?P[0-9]{3})(?P[0-9]{4})","410423199909264687").groupdict()
print(result2) #{'province': '410', 'city': '423', 'birthday': '1999'}

#split的用法,解释将字符串按特定的要求进行分割
result3 = re.split("[0-9]","wo12de3she678bao")
print(result3) #['wo', '', 'de', 'she', '', '', 'bao']
result4 = re.split("[0-9]+","wo12de3she678bao")
print(result4) #['wo', 'de', 'she', 'bao']

#sub的用法,sub(pattern,repl,string,count=0,flags=0)
#其中pattern需要填写的正则表达式,repl表示替换的值,string匹配的字符串,count替换的地方个数
result6 = re.sub("[0-9]+","=","wq12hgh8mmww56ssaa")
print(result6) #wq=hgh=mmww=ssaa
result7 = re.sub("[0-9]+","=","wq12hgh8mmww56ssaa",count=2)
print(result7) #wq=hgh=mmww56ssaa

反斜杠匹配

#正则中反斜杠的匹配,需要4个反斜杠
result8 = re.search("\\\\","wq12hg\h8mmww56ssaa").group()
print(result8) #\

flags的用法

#flags的用法,IGNORECASE忽略大小写-写法为re.I、MULTILINE匹配多行模式-写法为re.M、DOTALL点任意匹配模式-写法为re.S
num2 = re.search("[a-z]+","asDjWnQ").group()
print("添加flags前结果",num2) #添加flags前结果 as
num1 = re.search("[a-z]+","asDjWnQ",flags=re.I).group()
print("添加flags后结果",num1) #添加flags后结果 asDjWnQ
#匹配多行模式
num3 = re.search("^a","\nasd23",flags=re.M).group()
print(num3) #a
num4 = re.search("^a","\nasd23").group()
print(num4) #程序报错
#匹配任意字符re.S
num5 = re.search("^.+","\nabc\nhgf").group()
print(num5) #程序报错
num6 = re.search("^.+","\nabc\nhgf",flags=re.S).group()
print(num6) #abc
            #hgf

你可能感兴趣的:(正则表达式讲解(详细备注))