1.re.findall("a","ab")
a:匹配规则 ab:要匹配的内容
结果以列表的形式返回:[‘a‘]
2.元字符:".","^","$","*","+","?","{ }","[ ]","|","( )","\"
".":匹配除换行符“\n”以外任何字符;
import re A = re.findall(‘ab.d‘,"abcdefg") B = re.findall(‘ab.e‘,"abcdefg") print(A) #[‘abcd‘] print(B) #[]
"^":匹配字符串开头
import re A = re.findall(‘^ab‘,"abcdefg") B = re.findall(‘^bc‘,"abcdefg") print(A) #[‘ab‘] print(B) #[]
"$":匹配字符串结尾
import re A = re.findall(‘ab$‘,"abcdefg") B = re.findall(‘fg$‘,"abcdefg") print(A) #[] print(B) #[‘fg‘]
"*":匹配前一个字符0次或无限次
import re A = re.findall(‘ab*‘,"abbdefg") B = re.findall(‘ab*‘,"acdefg") print(A) #[‘abb‘] print(B) #[‘a‘]
"+":匹配前一个字符1次或无限次
"?":匹配前一个字符0次或1次
"{n}":匹配前一个字符n次
"{n,m}":匹配前一个字符n至m次(闭区间)
"[ab]":匹配a或b
import re A = re.findall(‘a[bc]d‘,"abcdefg") B = re.findall(‘a[bc]d‘,"abdefg") C = re.findall(‘a[bc]d‘,"acdefg") print(A) #[] print(B) #[‘abd‘] print(C) #[‘acd‘]
"\":1.反斜杠后面跟元字符,使元字符去除特殊功能
2.后面跟普通字符,产生特殊功能
3.引用序列对应的字组所匹配的字符串
原文:https://www.cnblogs.com/omg-hxy/p/9097925.html