一、正则表达式含义记录文本规则的代码字符串处理工具注意需要导入re模块特点语法比较复杂可读性较差通用性很强适用于多种编程语言步骤导入re模块使用match 方法进行匹配操作re.match() 能匹配出以xxx开头的字符串如果起始位置没有匹配成功返回Nonere.match(pattern, string, flags0)pattern: 匹配的正则表达式string: 要匹配的字符串注意match是匹配字符串开头匹配不到就没有且匹配的是表达式的整体如果男上一步数据匹配成功使用group()提取数据import re # re.match(pattern, string, flags0) # pattern : 匹配的正则表达式 # string : 要匹配的字符串 # flags : 匹配标志 s re.match(星,星月夜星) # 匹配字符串开头 print(s) print(s.group()) # re.Match object; span(0, 1), match星 # 星二、匹配单个字符字符功能.匹配任意1个字符除了\n)[]匹配[]中列举的字符\d匹配数字即0-9\D匹配非数字即不是数字\s匹配空白即空格、tab键\S匹配非空白\w匹配单词字符即a-z、A-Z、0-9、_\W匹配非单词字符import re res2 re.match([he],hello) print(res2.group()) # h res3re.match([1-9],1234) print(res3.group()) # 1 res4re.match([a-zA-Z],Hello) #a-zA-Z代表列举出所有大小写字母 print(res4.group()) # H # \ d 代表数字 res5re.match(\d,91234) print(res5.group()) # 9 # \ D 代表非数字 res6re.match(\D,。a1234) print(res6.group()) # 。 # \ s 代表空白字符/tab键代表两个空格 res7re.match(\s., hello) print(res7.group()) # h # \ S 代表非空白字符 res8re.match(\S,1hello) print(res8.group()) # 1 # \ w 代表字母数字下划线汉字 res9re.match(\w,你好_hello) print(res9.group()) # _ # \ W 代表非字母数字下划线 res10re.match(\W,/hello) print(res10.group()) # /三、匹配多个字符字符功能*匹配前一个字符出现 0 次或者无限次即可有可无匹配前一个字符出现 1 次或者无限次即至少有 1 次匹配前一个字符出现 1 次或者 0 次即要么有 1 次要么没有{m}匹配前一个字符出现 m 次{m,n}匹配前一个字符出现从 m 到 n 次import re # * 匹配前一个字符出现 0 次或者无限次即可有可无 resre.match(\w*,hello world) res1re.match(.*,hello world) print(res.group()) # hello print(res1.group()) # hello world # 匹配前一个字符出现 1 次或者无限次即至少一次 res2re.match(\d,11hello world) print(res2.group()) # 11 # ? 匹配前个字符出现 0 次或者 1 次 res3re.match(\w?,hello world) print(res3.group()) # h # {m} 匹配前一个字符 m 次 res4re.match(\w{2},hello world) print(res4.group()) # he # {m,n} 匹配前一个字符 m 到 n 次,必须符合mn res5re.match(\w{1,9},hello world) print(res5.group()) # hello四、匹配开头结尾字符功能^匹配字符串开头[^x]表示匹配非x的字符,取反$匹配字符串结尾import re # ^ 匹配字符串开头表示对……取反 # 注意 ^在[]中表示不匹配的字符,即[^p]表示匹配非p的字符 res re.match(^py,python) #以py开头 res1 re.match([^p],python) print(res.group()) # py # print(res1.group()) 报错AttributeError: NoneType object has no attribute group # $ 匹配字符串结尾 res2 re.match(.*n$,python) print(res2.group()) # python五、匹配分组字符功能|匹配左右任意一个表达式(ab)将括号中字符作为一个分组\num引用分组 num 匹配到的字符串(?P)分组起别名(?Pname)引用别名为 name 分组匹配到的字符串import re #1. | 匹配左右任意一个表达式 res re.match((python|java),python) res1 re.match((.|/d),123) print(res.group()) print(res1.group()) #2. (abc) 将括号中字符作为一个分组 res2 re.match(\w*(163|qq|126).com,123163.com) print(res2.group()) #3. \num 匹配分组num匹配到的字符串 ----经常在匹配标签时被使用 # res3 re.match((\w*)\w.*/\\1,htmlhello world/html) ---\\转义字符 # res3 re.match(r(\w*)\w.*/\1,htmlhello world/html) # \1表示匹配的分组1r表示取消转义 res3 re.match(r(\w*)(\w*)\w.*/\2/\1,htmlbodyhello world/body/html) print(res3.group()) #注意从外到内排序编号从1开始 #4. (?Pname) 分组起别名 #5. (?Pname) 引用别名为 name 分组匹配到的字符串 res4 re.match(r(?Ptag\w*)(?Ptag2\w*)\w.*/(?Ptag2)/(?Ptag),htmlbodyhello world/body/html) print(res4.group()) # python # 1 # 123163.com # htmlbodyhello world/body/html # htmlbodyhello world/body/html举例import re # 匹配网址 前缀一般是www后缀.com、.cn、.org等 li [www.baidu.com,www.google.com,http.jd.cn,www.python.org] # res re.match(rwww(\.)\w*\1(com|cn|org),www.baidu.com) # print(res.group()) for i in li: res re.match(rwww(\.)\w*\1(com|cn|org),i) if res: # print(i) print(res.group()) else: print(f{i}这个网址格式错误) # www.baidu.com # www.google.com # http.jd.cn这个网址格式错误 # www.python.org六、高级用法--正则函数re.match()从字符串开头匹配只找第一个符合的单个字符re.search()扫描整个字符串并返回第一个成功匹配的对象如果匹配失败则返回None;在整个字符串找第一个符合的单个字符匹配到就停re.findall()以**列表**形式返回整个字符串中所有匹配到的字符串import re res re.search(python,python hello world) print(res.group()) # re.findall(pattern, string, flags0) 搜索字符串返回所有匹配的字符串,返回一个列表 res1 re.findall(python,python hello worldpython) print(res1) # python # [python, python] # 总结 # match()从头开始匹配匹配成功返回match对象通过group()进行提取匹配失败就返回None只匹配一次。 # search()从头到尾匹配匹配成功返回第一个成功匹配的对象通过group()进行提取匹配失败返回None只匹配一次。 # findall()从头到尾匹配匹配成功返回一个列表匹配所有匹配成功的数据不需要通过group()进行提取。re.sub() : 将匹配到的数据进行替换。re.sub(pattern, repl, string, count0, flags0)pattern : 匹配的正则表达式(代表需要被替换的也就是字符串里面的旧内容repl : 替换的字符串新内容string : 要匹配的字符串count : 替换的次数默认为0表示替换所有匹配的import re res re.sub(python,java,python hello worldpython) print(res) res1 re.sub(\d,*,今天是第1天明天是第2天了,1) print(res1) # java hello worldjava # 今天是第*天明天是第2天了split()根据匹配进行切割字符串并返回一个列表。re.split(pattern, string, maxsplit0, flags0)pattern : 正则表达式 分割的符串string : 要匹配的字符串maxsplit : 指定最大分割次数分割的次数默认为0表示分割所有匹配的import re res re.split(|,python hello world) # | 匹配左右任意一个表达式 res1 re.split(o,python hello world) res2 re.split(o, python hello world,1) print(res) print(res1) print(res2) # [, p, y, t, h, o, n, , h, e, l, l, o, , w, o, r, l, d, ] # [pyth, n hell, w, rld] # [pyth, n hello world]七、贪婪与非贪婪贪婪匹配在满足匹配条件时会匹配尽可能长的字符串。在正则表达式中这是默认的匹配模式。非贪婪匹配在满足匹配条件时会匹配尽可能短的字符串。在正则表达式中通过在量词如*,,?,{n,}后添加一个?来表示非贪婪匹配import re # 贪婪匹配尽可能多的匹配默认贪婪匹配 res re.match(em*,emmmmmmm……) print(res.group()) # 非贪婪匹配尽可能少的匹配 res1 re.match(em*?,emmmmmmm……) print(res1.group()) res2re.match(m{1,5},mmmmmmm……) print(res2.group()) res3re.match(m{1,5}?,mmmmmmm……) print(res3.group()) # emmmmmmm # e # mmmmm # m八、原生字符串Python中字符串前面加上 r 表示原生字符串import re print(rfives\tar) # 取消转义 res re.match(\\\\,\game) # 正则表达式中匹配字符串中的字符\,需要\\\\ res1 re.match(r\\\\,r\\game)# 加入原生字符串r\\代表\ print(res.group()) print(res1.group())