正则表达式,用来匹配字符串
import re
# 基础:String自带方法
tag_str = "auto test"
a = tag_str.find("to")
print(a) #2, 返回搜索字符串的索引
#1. findall
# a = re.findall(pattern, string, flags)
# pattern: 匹配表达式
# string:目标字符串
# flags: 修改匹配规则
#匹配t开头,间隔1个字符,s结尾
a = re.findall("t\w{1}s", "auto test")
print(a) #['tes']
a = re.findall("bc", "abcd")
# 2.元字符
# 2.1 .通配符
print(a) #['bc']
a = re.findall("b.d", "abcd")
print(a) #.只能代替一个字符['bcd']
a = re.findall("b.c", "ab\ncd")
print(a) #[],不能匹配\n
a = re.findall("b.c", "ab\tcd")
print(a) #['b\tc']
# 2.2 ^只在最开始的位置匹配
a = re.findall("^a.c", "abcd")
print(a) #['abc']
b = re.findall("^b.d", "abcd")
print(b) #[]
# 2.3 $只在结尾匹配
a = re.findall(".d$", "abcd")
print(a) #['cd']
b = re.findall(".c$", "abcd")
print(b) #[]
# 2.4 *重复匹配[0,+∞],最少有0个重复字符
a = re.findall(".*c", "abcd")
print(a) #['abc'],重复匹配占位符
b = re.findall("a*b", "aabc")
print(b) #['aab'],重复匹配a
c = re.findall(".*.", "aabc")
print(c) #['aabc'],匹配整个字符
# 2.5 +重复匹配[1,+∞],最少有一个重复字符
a = re.findall(".+", "abcd")
print(a) #['abcd']
a = re.findall(".*", "abcd")
print(a) #['abcd', '']
a = re.findall("a+b", "abcbabb")
print(a) #['ab', 'ab']
# 2.6 ?[0,1],0或1的重复字符
a = re.findall("a?b", "abcadb")
print(a) #['ab', 'b'],b前面又0个或者1个a
# 2.7 {}任意重复次数的
a = re.findall("a{3}b", "caaab")
print(a) #['aaab'],相当于b前面有3个a
a = re.findall("a{1}b", "acb")
print(a) #[]
a = re.findall("a{1,3}b", "abaabaaabaaaab")
print(a) #['ab', 'aab', 'aaab', 'aaab'],1~3个a
"""
总结
*=={0,+∞}
+=={1,+∞}
?=={0,1}
"""
# 2.8 []字符集(普通字符和元字符)
#取消元字符的特殊功能
a = re.findall("a[{]s", "a{safsf")
print(a) #['a{s']
a = re.findall("a[c,d,b]e", "aceadeabeabcd")
print(a) #['ace', 'ade', 'abe'],a后面可以是c或d或b
a = re.findall("[com]", "1.qq.com")
print(a) #['c', 'o', 'm']
a = re.findall("[1-9,a-z,A-Z]", "1Ab")
print(a) #['1', 'A', 'b']
# 2.9 \
# 2.9.1 去除元字符功能
a = re.findall("a\{1\}", "a{1}23")
print(a) #['a{1}']
a = re.findall(r"\\", "abc\d")
print(a) #['\\'],匹配一个\会打印出来的\\
#re.error,因为re中的\有特殊功能,所以需要再加\\实现
#python解释器中\也有特殊意义,所以也会返回\\
# 2.9.2 加特殊字符实现特殊功能
# \d 匹配数字[1,9]
a = re.findall("a\d*", "a123")
print(a) #['a123']
# \D 匹配非数字[1,9]
a = re.findall("a\D", "abc")
print(a) #['ab']
# \s 匹配空白符 [\t\n\r\f\v]
a = re.findall("\sabc", "d abc")
print(a) #[' abc']
# \S 匹配非空白符
a = re.findall("\Sabc", "efdabc")
print(a) #['dabc']
# \w 匹配任何字母数字字符[1~9,a~z,A~Z]
a = re.findall("\wabc", "dabc1abc")
print(a) #['dabc', '1abc']
# \W 匹配任何非字母数字字符^[1~9,a~z,A~Z]
a = re.findall("\Wabc", "dabc1abc abc")
print(a) #[' abc']
# \b 匹配单词特殊边界,比如a后面是单词边界
a = re.findall(r"a\b", "ab ,a acd")
print(a) #['a']
#2. search 找到符合条件的第一个
a = re.findall("\d{2}", "123456")
print(a) #['12', '34', '56']
a = re.search("\d{2}", "123456")
print(a) #<re.Match object; span=(0, 2), match='12'>
print(a.group()) #12,如果没匹配到,调取group会报错
# 2.10 ()
#分组
a = re.search("(as).", "asc")
print(a.group()) #asc
a = re.search("(as)|b", "bas")
print(a.group()) #b
a = re.search("(?P<name>F.)","Frank shi123")
print(a.group('name')) #Fr
#P<name>,给找到的数据取名字
# 3.match 只在字符串开始匹配
a = re.match("asd", "vasdcd")
print(a) #None
# 4.split
a = re.split('[c,d]', 'acbda')
print(a) #['a', 'b', 'a']先用c分割,然后再用d分割
# 5.sub等同于replace
a = re.sub("a..d", "1", "abcda")
print(a) #1a
# 6.compile
a = re.compile("\.com") #正则表达式对象
r = a.findall("163.com")
print(r) #['.com']
# 模式修饰符
# I:匹配时忽略大小写
#常规
s1 = "aBcdefg"
r1 = re.findall("abc.*g", s1)
print(r1) #[]
#添加模式修饰符
r11 = re.findall("abc.*g", s1, re.I)
print(r11) #['aBcdefg']
# S:匹配换行符
#常规
s = """abc
def
g"""
print(re.search("abc.*g", s)) #存在换行符,所以没有匹配到 None
#添加模式修饰符
print(re.search("abc.*g", s, re.S).group()) #abc\ndef\ng
#其他修饰符
# re.M 多行匹配,影响 ^ 和 $
# re.L 做本地化识别(locale-aware)匹配
# re.U 根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.
# re.X 该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。