import re
#匹配abc
m = re.findall("abc", "aabbccabccbaccbaaabcabc")
print(m)
#匹配一位数字,digit
m = re.findall("\d", "123abc1a2b3c1b2c3a")
print(m)
#匹配四位数字
m = re.findall("\d\d\d\d", "123abc1234ab12345c")
print(m)
#贪婪匹配
m = re.findall("(.*)", "hello")
print(m)
m = re.findall("(.*)", "helloworld")
print(m)
#非贪婪匹配
m = re.findall("(.*?)", "helloworld")
print(m)
#除换行符以外的所有字符
m = re.findall(".", "asdd\ndwjh\r")
print(m)
#转义字符
m = re.findall("\.", "a.c")
print(m)
#字符集,匹配中括号中任意一个字符
m = re.findall("a[bc]d", "abcdabdacdabbsdad")
print(m)
#数字
m = re.findall("\d", "abc1a2a32b")
print(m)
#非数字
m = re.findall("\D", "abc1a2a32b#")
print(m)
#空白字符
m = re.findall("\s", "abc a\na\r asd")
print(m)
#非空白字符
m = re.findall("\S", "abc a\na\r asd")
print(m)
#数字&字母
m = re.findall("\w", "ald!@n12\n1s\rk13")
print(m)
#非数字*字母
m = re.findall("\W", "ald!@n12\n1s\rk13")
print(m)
#匹配开头
m = re.findall("^abc", "abccbaabc")
print(m)
#匹配结尾
m = re.findall("cba$", "cbaabccba")
print(m)
#不区分大小写re.I
m = re.findall("abc", "AbcaBcabC", re.I)
print(m)
#匹配换行符&组合使用re.I
s = "hello\nworld"
m = re.findall(r"(.*)", s, re.S | re.I)
print(m)
#匹配多行
m = re.findall("^abc", "abc\nabc")
print(m)
m = re.findall("^abc", "abc\nabc", re.M)
print(m)
#匹配0或1个
m = re.findall("ab?", "abbbabbaaab")
print(m)
#匹配至少1个
m = re.findall("ab+", "abbbabbaaab")
print(m)
#匹配至少0个
m = re.findall("ab*", "abbbabbaaab")
print(m)
#实例:匹配邮箱
mail = "[email protected]"
m = re.findall("\w+@\w+\.com", mail)
print(m)
#先编译后使用
a = re.compile("\w+@\w+\.com")
m = a.findall("[email protected]")
n = a.findall("[email protected]")
print(m, n)
输出结果:
['abc', 'abc', 'abc']
['1', '2', '3', '1', '2', '3', '1', '2', '3']
['1234', '1234']
['hello']
['hello