# 除换行符之外的任意字符
import re
str = "Hi \n _12!"
print(str)
"""
Hi
_12!
"""
# 除换行符之外的任意字符
print(re.findall(".", str))
# ['H', 'i', ' ', ' ', '_', '1', '2', '!']
# 连续2个非换行的字符
print(re.findall("..", str))
# ['Hi', ' _', '12'] 注意:不会重复匹配字符的
# 连续3个非换行的字符
print(re.findall("...", str))
# ['Hi ', ' _1']
# 连续4个非换行的字符
print(re.findall("....", str))
# [' _12']
字母、数字、下划线
小写w,表示 字母,数字,下划线
大写W,表示 非字母,非数字,非下划线
import re
str = "Hi \n _12!"
# 匹配1个
print(re.findall("\w", str))
# ['H', 'i', '_', '1', '2']
# 匹配连续的2个
print(re.findall("\w\w", str))
# ['Hi', '_1']
# 匹配连续的3个
print(re.findall("\w\w\w", str))
# ['_12']
import re
str = "Hi \n _12!"
# 数字
print(re.findall("\d", str))
# ['1', '2']
# 空格、换行符
print(re.findall("\s", str))
# [' ', '\n', ' ']
# NOT 字母、数字、下划线
print(re.findall("\W", str))
# [' ', '\n', ' ', '!']
# NOT 数字
print(re.findall("\D", str))
# ['H', 'i', ' ', '\n', ' ', '_', '!']
# NOT 空格、换行符
print(re.findall("\S", str))
# ['H', 'i', '_', '1', '2', '!']
# 两个空格+小写w
print(re.findall("\s\s\w", str))
# ['\n _']
print(re.findall("\w \s", str))
#['i \n']
精准匹配
import re
str2 = "Hello World 123"
# 精准匹配1个e
print(re.findall("e", str2))
# ['e']
# 精准匹配2个l
print(re.findall("ll", str2))
# ['ll']
# 匹配1个l+小写w
print(re.findall("l\w", str2))
# ['ll', 'ld']
方括号,任意匹配
import re
str2 = "Hello World 12d31da"
# 匹配方括号中的字符,只要字符中出现,都会被单个匹配到
print(re.findall("[abcd]", str2))
# ['d', 'd', 'd', 'a']
# 匹配方括号中的数字,只要字符中出现,都会被单个匹配到
print(re.findall("[12]", str2))
# ['1', '2', '1']
# 匹配结果为两个数字,同时满足方括号中的数字
print(re.findall("[12][12]", str2))
# ['12d', '1da']
# 匹配方括号中的任意数字,不能重复匹配,并且后面跟2个字符(非换行符)
print(re.findall("[12]..", str2))
# ['12d', '1da']
# 匹配方括号中 a-f 的任意字符
print(re.findall("[a-f]", str2))
# ['e', 'd', 'd', 'd', 'a']
# 匹配方括号中 a-f 的任意字符,结果为方括号中的任意两个字符组合
print(re.findall("[a-l][a-l]", str2))
# ['el', 'ld', 'da']
# 匹配方括号中 a-0-9 的任意数字
print(re.findall("[0-9]", str2))
# ['1', '2', '3', '1']
# 匹配方括号中的 0-2 或者 a-e 的字符或数字,结果为1个字符
print(re.findall("[0-2a-e]", str2))
# ['e', 'd', '1', '2', 'd', '1', 'd', 'a']
# 匹配方括号中的 0-2 或者 a-e 的字符或数字,结果为2个字符
print(re.findall("[0-2a-e][0-2a-e]", str2))
# ['12', '1d']
# ^为取反操作,匹配小写字母 非a-z 的字符(排除换行符)
print(re.findall("[^a-z]", str2))
# ['H', ' ', 'W', ' ', '1', '2', '3', '1']
# ^为取反操作,匹配小写字母 非a-z 的字符,并且后面跟随两个除换行符外的字符
print(re.findall("[^a-z]..", str2))
# ['Hel', ' Wo', ' 12', '31d']
* 表示0次或更多
{3} 表示3个连续的数字
+ 代表重复1次或更多
import re
str3 = "122333 abbccc"
------------------------------------------------------------
# 匹配连续的3个数字, {3} 大括号+3,表示3个连续的数字
print(re.findall("\d{3}", str3))
# ['122', '333']
# 匹配连续的三个字符,排除换行符
print(re.findall("\w{3}", str3))
# ['122', '333', 'abb', 'ccc']
# 匹配字符1重复0次或更多,*表示0次或更多
print(re.findall("1*", str3))
# ['1', '', '', '', '', '', '', '', '', '', '', '', '', '']
# 匹配字符1重复0次或更多,后面跟一个2;
print(re.findall("1*2", str3))
# ['12', '2']
# 匹配字符1重复0次或更多,后面跟一个3;(1和3中间的2不符合,*表示0次或更多,所以这里输出了3个3)
print(re.findall("1*3", str3))
# ['3', '3', '3']
# 匹配字符2重复0次或更多,后面跟一个3;
print(re.findall("2*3", str3))
# ['223', '3', '3']
------------------------------------------------------------
# 匹配字符1,重复1次或更多
print(re.findall("1+", str3))
# ['1']
# 匹配字符1,重复1次或更多,后面跟1个2
print(re.findall("1+2", str3))
# ['12']
# 匹配字符1,重复1次或更多,后面跟1个3
print(re.findall("1+3", str3))
# []
------------------------------------------------------------
# 匹配数字出现0次或更多,因为不能重复匹配,所以这里输出第一个数字后,后面都是空字符
print(re.findall("\d*", str3))
# ['122333', '', '', '', '', '', '', '', '']
# 1个数字重复1次或更多
print(re.findall("\d+", str3))
# ['122333']
# 匹配出现0次或更多次,后面跟一个3
print(re.findall("\d*3", str3))
# ['122333']
# 匹配出现1次或更多次,后面跟一个3
print(re.findall("\d+3", str3))
# ['122333']