# ### 正则表达式 # findall 匹配字符串中相应内容, 返回列表 import re ''' lst = re.findall(正则表达式,要匹配的字符串) ''' ''' \d 匹配数字 \D 匹配非数字 ''' lst = re.findall("\d","sadfsdfsa 98&*^&^&92sdf3sdf839 sdf_as 神秘男孩 89") print(lst) lst = re.findall("\D","sadfsdfsa 98&*^&^&92sdf3sdf839 sdf_as 神秘卫星 89") print(lst) """ \w 字母,数字,下划线,包含中文(正则函数当中支持中文的匹配) \W 匹配非字母或数字或下划线 """ lst = re.findall("\w","09090sdf909sdf_+()()&*&*%^%^你好sd") print(lst) lst = re.findall("\W","09090sdf909sdf_+()()&*&*%^%^你好sd") print(lst) """ \s 匹配任意空白符 \S 匹配任意非空白符 """ strvar = """ """ lst = re.findall("\s"," ") print(lst) lst = re.findall("\s",strvar) print(lst) lst = re.findall("\S"," lklkl sdfsdf sdfsdf") print(lst) """ \n 匹配换行 \t 匹配一个制表符 """ strvar = """ 今天 拉 肚 子 """ lst = re.findall(r"\n",strvar) print(lst) lst = re.findall(r"\t",strvar) print(lst) # ###字符组练习 必须从字符组选一个,如果一个都没有则匹配不成功 lst = re.findall("[123]","s3dafsf2s1") print(lst) print(re.findall('a[abc]b','aab abb acb adb')) print(re.findall('a[0123456789]b','a1b a2b a3b acb ayb a9090909009b')) # 优化版:0123456789 => 0-9 等价 print(re.findall('a[0-9]b','a1b a2b a3b acb ayb a9090909009b')) print(re.findall('a[abcdefg]b','a1b a2b a3b acb ayb adb aabababbababb')) #['acb', 'adb'] # 优化版:a b c d .... z => a-z print(re.findall('a[a-g]b','a1b a2b a3b acb ayb adb aabababbababb')) print(re.findall('a[a-z]b','a1b a2b a3b acb ayb adb aabababbababb')) print(re.findall('a[ABCDEFG]b','a1b a2b a3b aAb aDb aYb')) # 优化版: A B C ... Z => A-Z print(re.findall('a[A-G]b','a1b a2b a3b aAb aDb aYb')) print(re.findall('a[A-Z]b','a1b a2b a3b aAb aDb aYb')) print(re.findall('a[0-9a-zA-Z]b','a-b aab aAb aWb aqba1b')) # 优化版: a-zA-Z => A-z 可以匹配所有大小写,但是有缺陷,一些特殊符号也匹配到了 print(re.findall('[A-z]','(')) print(re.findall('[0-z]','9')) # 语法上允许,但是别这么用,没意义 print(re.findall('a[0-9][*#/]b','a1/b a2b a29b a56b a456bab')) # ^ 字符组中的^ 代表除了的意思 print(re.findall('a[^-+*/]b',"a%b ccaa*bda&bd")) # 单独匹配一个特殊字符,用\让他的意思失效 转义字符 print(re.findall('a[\^]b',"a^b"))
来源:https://www.cnblogs.com/huangjiangyong/p/10924350.html