# 元字符+,*遇到?后就会变为贪婪匹配 print(re.findall('abc+?','abcccccc')) #['abc'] print(re.findall('abc*?','abcccccc')) #['ab'] print(re.findall('abc{1,4}','abcccabc')) #['abccc', 'abc'] print(re.findall('abc{1,4}?','abcccabc')) #['abcc', 'abc'] #--------------------------------------------------------------------- #元字符之字符集[] 在字符集里面有功能的字符 - ^ print(re.findall("[abc]","sdfgbahjkkk")) #若是字符集的前后没有字母,就会分别匹配a,b,c ['b', 'a'],否在就字符集里选择一个字母和外面的连接使用 print(re.findall("a[a-z]","fwefaefewaasfwe")) #['ae', 'aa'] print(re.findall("a[.*+]","a.b*c+")) #在字符集里的符号就是普通符号 ['a.'] print(re.findall("[^ab]","fgddab")) #['f', 'g', 'd', 'd'] print(re.findall("[1-9]","vsd1fdv2sdvsd23")) #['1', '2', '2', '3'] print(re.findall("d[1-9]","vsd1fdv2sdvsd23")) #['d1', 'd2'] print(re.findall("[d]","af1fsdf2ffsf1")) #['1', '2', '1'] print(re.findall("[1-9]","af1fsdf2ffsf1")) #['1', '2', '1']
print(re.findall("[^W1-9a-z]","f我1e@f们21在ew2一%&e起1f")) #['我', '们', '在', '一', '起'] #^W 就是去掉符号 再去掉数字和字母
#转义字符 #反斜杠后面跟元字符会失去特殊功能 如* print(re.findall("*","vdv*vdd")) print(re.findall(".","vdf.v*vdd")) print(re.findall("?","vd?f.v*vdd")) print(re.findall("^","vdf.v*vd^d")) print(re.findall("[ab]","vdf.v*v[ab]dd")) print(re.findall("{2,3}","vdf.v*v{2,3}dd")) print(re.findall("$","vdf.v*v$dd")) print(re.findall("+","vdf.v+*vdd"))
反斜杠后边跟元字符去除特殊功能,比如. 反斜杠后边跟普通字符实现特殊功能,比如d d 匹配任何十进制数;它相当于类 [0-9]。 D 匹配任何非数字字符;它相当于类 [^0-9]。 s 匹配任何空白字符;它相当于类 [ fv]。 S 匹配任何非空白字符;它相当于类 [^ fv]。 w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。 W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_] 匹配一个特殊字符边界,比如空格 ,&,#等
#反斜杠后面跟普通字母可以实现特殊功能 如d w s print(re.findall("d","12f345fs56dsfs")) #只匹配数字 print(re.findall("[1-9]","12f345fs56dsfs")) print(re.findall("[^D]","12f345fs56dsfs")) print(re.findall("[^a-z]","12f345fs56dsfs")) #--------------------------------------------------------------------- print(re.findall("D","12f345fs56dsfs")) #只匹配字母 print(re.findall("[a-z]","12f345fs56dsfs")) print(re.findall("[^1-9]","12f345fs56dsfs")) print(re.findall("[^d]","12f345fs56dsfs"))
print(re.findall("w","12f345fs56dsfs")) #匹配所有的字母和数字 print(re.findall("[^W]","12f345fs.,56dsfs")) print(re.findall("[1-9a-z]","12f345fs56dsfs")) #--------------------------------------------------------------------- print(re.findall("W","12f345f;'s56dsfs")) #匹配符号 print(re.findall("[^w]","12f345fs,/56dsfs")) print(re.findall("[^1-9a-z]","12f345fs,/56dsfs"))
#s 匹配空格 print(re.findall("[s]","a bcd")) #[' '] print(re.findall("[^s]","abcd")) #['a', 'b', 'c', 'd'] print(re.findall("S","abc d")) #['a', 'b', 'c', 'd']
print(re.findall("[^s]","ff fse ")) #除了空格 ['f', 'f', 'f', 's', 'e'] print(re.findall("v\\h","sdvhyu")) #匹配斜杠 ['v\h'] print(re.findall(r"v\h","sdvhyu")) #匹配斜杠 ['v\h'] # 表示匹配一个特殊字符边界 print(re.findall(r"ber","sfsberhj")) #匹配 ber print(re.findall(r"ber","sfs:berhj")) #匹配 ber