说明:下方的xyz均为假设的普通字符,不是正则表达式的元字符
(xyz) 匹配小括号内的xyz(作为一个整体去匹配)
x? 匹配0个或1个x
x* 匹配0个或任意多个x(.*表示匹配0个或任意多个字符(换行符除外))
x+ 匹配至少一个x
x{n} 匹配确定的n个x(n是一个非负数的整数)
x{n,} 匹配至少n个x
x{n,m} 匹配至少n个,最多m个x(m,n是一个非负数的整数)注意n <= m
x|y 匹配的是x或y,|表示或
import re
print(re.findall(r"(sunck)","sunckgood is a good man,sunck is a nice man"))
print(re.findall(r"o?","sunckgood is a good man,sunck is a nice man")) print(re.findall(r"a*","aaabaa")) print(re.findall(r".*","aaabaa")) print(re.findall(r"a+","abaa")) print(re.findall(r"a{3}","aaabaaaacaa"))
print(re.findall(r"a{3,}","aaabaaaacaa")) print(re.findall(r"a{3,6}","aaabaaaacaadaaaaaaaaa"))
print(re.findall(r"((s|S)unck)","sunck--Sunck")) str = "sunck is a good man!sunck is a nice man!sunck is a very handsome man" print(re.findall(r"^sunck.*man$",str))