re 正则
Posted womenzt
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了re 正则相关的知识,希望对你有一定的参考价值。
import re
# print(re.findall("\w","宝元-alex_dsb123日莫")) # 返回的是列表***
# 字母 数字 中文 下划线
# print(re.findall("\W","宝元-alex_dsb123日莫"))***
# 非字母、数字、中文、下划线
# print(re.findall("\s","alex dsb rimo cjb")) # 匹配空格
# print(re.findall("\S","alex dsb rimo cjb")) # 匹配非空格
# print(re.findall("\d","十10⑩")) # 匹配数字 ***
# print(re.findall("\D","十10⑩")) # 匹配非数字 ***
# print(re.findall("\Aa","alex")) # 匹配以什么开头
# print(re.findall("^a","alex")) ***匹配以什么开头
# print(re.findall("b\Z","alexdsb")) # 匹配以什么结尾
# print(re.findall("x$","alex")) ***匹配以什么结尾
# print(re.findall("\n","alex\nwusir")) *
# print(re.findall("\t","alex\twusir")) *
# print(re.findall("alex","alex\twusir")) *
# print(re.findall("alex","alex\twusiralex")) *
# print(re.findall("a.c","abc,aec,a,c,a c")) # .匹配任意除(\n)***
# print(re.findall("^a.+c$","abc,aec,a,c,a d")) #***
# print(re.findall("^a....c$","aaecdc")) #一个点只占一个位置
# print(re.findall("^a.c$","abc"))
# print(re.findall("a.c","abc,aec,a\nc,a,c",re.DOTALL))
# print(re.findall("[0-9]",‘alex0123,日莫dsb,小黄人_229‘)) 匹配0-9之间的数字
# print(re.findall("[a-z]",‘alex0123,日莫dsb,小黄人_229‘)) 匹配a-z之间的字母
# print(re.findall("[A-Z]",‘alex0123,日莫DSB,小黄人_229‘)) 匹配A-Z之间的大写字母
# print(re.findall("[0-9 a-z A-Z]",‘alex0123,日莫DSB,小黄人_229‘)) 匹配数字、大小写字母
# print(re.findall("[-0-9]",‘alex0-123,日莫DSB,小黄人_229‘)) # 要取减号时只需放前面 放中间是区间
# [0-9] 取0-9之间的数字
# [^0-9] 取非0-9之间的数字
# print(re.findall("[^0-9]",‘123alex456‘)) # 非0-9的其他
# print(re.findall("[^0-9a-z]",‘123alex456‘)) #非0-9和a-z的其他=
# print(re.findall("a*","alex,aa,aaa,bbbaaa,aaaabbbbaa")) #匹配*左侧字符串0次或多次 贪婪匹配 ***
# print(re.findall("a+","alex,aa,aaa,bbbaaa,aaaabbbbaa")) #匹配+左侧字符串一次或多次 贪婪匹配
# print(re.findall("a?","alex,aa,aaa,bbbaaa,aaaabbbbaa")) #匹配?左侧1个或0个 非贪婪
# print(re.findall("a3","alex,aa,aaa,bbbaaa,aaaabbbbaa")) #指定查找的元素个数
# print(re.findall("[0-9]11","18280890650,17832303835,bbbaaa,aaaabbbbaa"))
# print(re.findall("a3,8","alex,aaaaabbbbaaaabbbbbaaa,aaaaaaaabb,ccccccddddaaaaa")) 指定匹配a个数的区间
# a|b 或
# print(re.findall("a|b","alexdsb"))
# ?:里层外层都要
# 括号是分组
# print(re.findall("a(.?)c","alc,aba,adc,a c")) # 匹配到的是括号里的东西
# print(re.findall("<a>(.+?)<c>","<a>alex<c> <a>ad<c>"))
# print(re.findall("<a>(.+)<c>","<a>alex<c> <a>ad<c>"))
# print(re.findall("<a>(.?)<c>","<a>alex<c> <a>ad<c>"))
# print(re.findall("<a>(?:.+?)</c>","<a>alex</c> <a>ad</c>")) #外层也匹配
# s = ‘alex_sb ale123_sb wu12sir_sb wusir_sb ritian_sb‘
# print(re.findall("(.+?)_sb",s))
# 面试题(重点)
# search 和match的区别
# search 从任意位置开始查找 匹配不到就返回None
# match 从头开始查看,如果不符合就不继续查找了 匹配不到就会报错
# group()进行查看
# print(re.search("a.+","lexaaaaa,bssssaaaaa,saaasaasa").group())
# print(re.match("a.+","alexaaaaa,bssssaaaaa,saaasaasa").group())
# split--分割
# print(re.split("[:;,.!#]",‘alex:dsb,wusir.djb#laoda‘))
# sub (替换)
# s = ‘alex:dsb,wusir.djb#laoda‘
# print(re.sub("d","e",s,count=1))
# compile 定义匹配规则
# s = compile("\w")
# print(s.findall(‘alex:dsb,wusir.djb#laoda‘))
# s = re.finditer("\w",‘alex:dsb,wusir.djb#laoda‘,) # 返回就是一个迭代器
# print(next(s).group())
# print(next(s).group())
# print(next(s).group())
# for i in s:
# print(i.group())
# import re
# ?p#(给分组命名)
# ret = re.search("<(?P<tag_name>\w+)>\w+</\w+>","<h1>hello</h1>")
# ret = re.search("<(?P<tag_name>\w+)>(?P<content>\w+)</\w+>","<h1>hello</h1>")
# print(ret.group("tag_name"))
# print(ret.group("content"))
#
# print(ret.group("tag_name"))
# print(ret.group())
# \. 没有任意的功能了
# s = "1-2*(60+(-40.35/5)-(-4*3))"
# print(re.findall("\d+",s))
# print(re.findall("\d+\.\d+|\d+",s))
# print(re.findall("-\d+\.\d+|-[0-9]|\d+",s))
以上是关于re 正则的主要内容,如果未能解决你的问题,请参考以下文章