re模块

import re   #正则就是做模糊匹配

print(re.findall("alex","zszalexzsz"))
#元字符 .:通配符,除了 都可以匹配
print(re.findall("al...ex","alzszex")) #一个点只代表一个字符

#元字符 ^ :只能在字符串开头匹配内容
print(re.findall("^al...ex","alzszexalzszex"))

#元字符 $ :只能在字符串结尾匹配内容
print(re.findall("al...ex$","alzszexalzszex"))

#元字符 * :重复符号,*前字符可重复匹配(0,∞)次
print(re.findall("alex*","zszalexxxxxx"))
print(re.findall("alex*","zszale"))

#元字符 + :重复符号,+前字符可重复匹配(1,∞)次,+前必须有一个字符
print(re.findall("alex+","zszalexxxxxx"))
print(re.findall("alex+","zszale"))

#元字符 ?:重复符号,?前字符可重复匹配(0,1)次
print(re.findall("alex?","alexxxale"))
print(re.findall("alex+?","alexxxale"))
print(re.findall("alex*?","alexxxale"))

#元字符 {}:重复符号,{0,} == * {1,} == + {0,1} == ?
print(re.findall("alex{0,6}","alexxxxxxx"))

#元字符 []:字符集
print(re.findall("q[a-z]","qxghj")) #q后面只要加a到z任意字母都能匹配成功
print(re.findall("q[a-z]*","qxghj9")) # * 表示重复前面的字符集
print(re.findall("q[0-9]*","q"))
print(re.findall("q[^a-z]","qx")) #^尖角号在[]内为非
print(re.findall("([^()]*)","18+(2*(69-3))")) #[]:特殊:-^ 找到括号开头,括号结尾,且中间没有括号的字符串

#元字符 :转义符,让元字符变成普通字符,让普通字符变得有意义
#d:匹配任何十进制数,相当于类[0-9]
print(re.findall("d","18+(2*(69-3))"))
print(re.findall("d+","18+(2*(69-3))"))

#D:匹配任何非数字字符,相当于类[^0-9]
print(re.findall("D","18+(2*(69-3))"))

#s:匹配任何空白字符,相当于类[ fv]
print(re.findall("s","z s___z")) #_表示抽象的空格

#S:匹配任何非空白字符,相当于类[^ fv]
print(re.findall("S+","zsz lhf z"))

#w:匹配任何字母数字字符,相当于类[a-zA-Z0-9]
print(re.findall("w","zsz123"))

#W:匹配任何非字母数字字符,相当于类[^a-zA-Z0-9]
print(re.findall("w","zsz123**//"))

#:匹配一个特殊字符边界,比如空格,&,#等
print(re.findall(r"I","I_am LIST")) # r 原生字符串 加上r表示字符串内容不做任何转义
print(re.findall("I\b","I_am LIST")) #在python这一层有意义,python解释器会把翻译成python内规定的内容,
而不再是re所定义的

#python转义re再转义
print(re.findall("c\\l","cl")) #python将c\\l经过转义,re得到c\l,re再将c\l进行转义得到cl,
-->['c\l'] 经过两层转义,python转义完传给re,re再转义

print(re.findall(r"c\l","cl")) #跳过python转义,只让re转义
-->['c\l']

#元字符 | :管道符 或
print(re.findall(r"ka|b","kab"))
-->['ka','b']

#元字符 ():分组符号
print(re.search("(?P<name>[a-z]+)(?P<age>d+)","alex36xialv33").group("name"))#若不加group 则search返回一个对象
-->alex

print(re.findall("(?P<name>[a-z]+)(?P<age>d+)","alex36xialv33")) #search只找一个,findall找出所有
-->[('alex','36'),('xialv','33')]

print(re.findall("[a-z]+d+","alex36xialv33"))
-->['alex36','xialv33']
#re的常用方法
#re.findall
#re.search
#re.match 同search,只会在开头匹配,用.group()取值

#re.split 分割
print(re.split("[ |]","hello abc|def"))
-->['hello','abc','def']

print(re.split("[ab]","asdbsads")) #先按a分割得到“”,“sdbs”,“ds”,再按b分割得到“”,“sd”,“s”,“ds”
-->["","sd","s","ds"]

#re.sub(old,new,str,count) 替换
print(re.sub("d+","A","zsz123lhf456",1))
-->zszAlhf456

#re.subn 替换,返回替换次数
print(re.subn("d+","A","zsz123lhf456"))
-->('zszAlhfA',2)

#re.compile() 编译,可以用多次
com = re.compile("d+")
print(com.findall("zsz123lhf456"))
-->['123','456']

#re.finditer 返回迭代器对象,用一条处理一条
print(re.finditer("d","zsz12lhf456"))
print(next(re.finditer("d","zsz12lhf456")).group())
-->1

#注意
print(re.findall("www.(baidu|163).com","www.baidu.com"))
-->['baidu']

print(re.findall("www.(?:baidu|163).com","www.baidu.com")) #?: 取消分组的优先级
-->['www.baidu.com']
原文地址:https://www.cnblogs.com/zhangsenzhen/p/9418541.html