【RE】正则,模糊匹配
re模块大家都叫它正则,那什么又是正则呢?
正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法。或者说:正则就是用来描述一类事物的规则。(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。
匹配模式举例
<span># ----------------匹配模式--------------------</span>
<span># 1,之前学过的字符串的常用操作:一对一匹配</span>
<span># s1 = ‘fdskahf太白金星‘</span>
<span># print(s1.find(‘太白‘)) # 7</span>
正则匹配:
W
<span># 单个字符匹配</span>
<span>import<span> re</span></span>
<span># \w 与 \W</span>
<span># print(re.findall(‘\w‘, ‘太白jx 12*() _‘)) # [‘太‘, ‘白‘, ‘j‘, ‘x‘, ‘1‘, ‘2‘, ‘_‘]</span>
<span># print(re.findall(‘\W‘, ‘太白jx 12*() _‘)) # [‘ ‘, ‘*‘, ‘(‘, ‘)‘, ‘ ‘]</span>
S
<span>import<span> re</span></span>
<span># \s 与\S</span>
<span># print(re.findall(‘\s‘,‘太白barry*(_ \t \n‘)) # [‘ ‘, ‘\t‘, ‘ ‘, ‘\n‘]</span>
<span># print(re.findall(‘\S‘,‘太白barry*(_ \t \n‘)) # [‘太‘, ‘白‘, ‘b‘, ‘a‘, ‘r‘, ‘r‘, ‘y‘, ‘*‘, ‘(‘, ‘_‘]</span>
D
<span>import<span> re</span></span>
<span># \d 与 \D</span>
<span># print(re.findall(‘\d‘,‘1234567890 alex *(_‘)) # [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘0‘]</span>
<span># print(re.findall(‘\D‘,‘1234567890 alex *(_‘)) # [‘ ‘, ‘a‘, ‘l‘, ‘e‘, ‘x‘, ‘ ‘, ‘*‘, ‘(‘, ‘_‘]</span>
^
<span>import<span> re</span></span>
<span># \A 与 ^</span>
<span># print(re.findall(‘\Ahel‘,‘hello 太白金星 -_- 666‘)) # [‘hel‘]</span>
<span># print(re.findall(‘^hel‘,‘hello 太白金星 -_- 666‘)) # [‘hel‘]</span>
$
<span># \Z、\z 与 $ </span>
<span># print(re.findall(‘666\Z‘,‘hello 太白金星 *-_-* \n666‘)) # [‘666‘]</span>
<span># print(re.findall(‘666\z‘,‘hello 太白金星 *-_-* \n666‘)) # []</span>
<span># print(re.findall(‘666$‘,‘hello 太白金星 *-_-* \n666‘)) # [‘666‘]</span>
\n
<span># \n 与 \t</span>
<span># print(re.findall(‘\n‘,‘hello \n 太白金星 \t*-_-*\t \n666‘)) # [‘\n‘, ‘\n‘]</span>
<span># print(re.findall(‘\t‘,‘hello \n 太白金星 \t*-_-*\t \n666‘)) # [‘\t‘, ‘\t‘]</span>
重复匹配
<span> <span>.<span> <span>?<span> <span>*<span> <span>+<span> <span>{<span>m<span>,<span>n<span>}<span> <span>.*<span> <span>.*?</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span># . 匹配任意字符,除了换行符(re.DOTALL 这个参数可以匹配\n)。</span>
<span># print(re.findall(‘a.b‘, ‘ab aab a*b a2b a牛b a\nb‘)) # [‘aab‘, ‘a*b‘, ‘a2b‘, ‘a牛b‘]</span>
<span># print(re.findall(‘a.b‘, ‘ab aab a*b a2b a牛b a\nb‘,re.DOTALL)) # [‘aab‘, ‘a*b‘, ‘a2b‘, ‘a牛b‘]</span>
<span># ?匹配0个或者1个由左边字符定义的片段。</span>
<span># print(re.findall(‘a?b‘, ‘ab aab abb aaaab a牛b aba**b‘)) # [‘ab‘, ‘ab‘, ‘ab‘, ‘b‘, ‘ab‘, ‘b‘, ‘ab‘, ‘b‘]</span>
<span># * 匹配0个或者多个左边字符表达式。 满足贪婪匹配 @@</span>
<span># print(re.findall(‘a*b‘, ‘ab aab aaab abbb‘)) # [‘ab‘, ‘aab‘, ‘aaab‘, ‘ab‘, ‘b‘, ‘b‘]</span>
<span># print(re.findall(‘ab*‘, ‘ab aab aaab abbbbb‘)) # [‘ab‘, ‘a‘, ‘ab‘, ‘a‘, ‘a‘, ‘ab‘, ‘abbbbb‘]</span>
<span># + 匹配1个或者多个左边字符表达式。 满足贪婪匹配 @@</span>
<span># print(re.findall(‘a+b‘, ‘ab aab aaab abbb‘)) # [‘ab‘, ‘aab‘, ‘aaab‘, ‘ab‘]</span>
<span># {m,n} 匹配m个至n个左边字符表达式。 满足贪婪匹配 @@</span>
<span># print(re.findall(‘a{2,4}b‘, ‘ab aab aaab aaaaabb‘)) # [‘aab‘, ‘aaab‘]</span>
<span># .* 贪婪匹配 从头到尾.</span>
<span># print(re.findall(‘a.*b‘, ‘ab aab a*()b‘)) # [‘ab aab a*()b‘]</span>
<span># .*? 此时的?不是对左边的字符进行0次或者1次的匹配,</span>
<span># 而只是针对.*这种贪婪匹配的模式进行一种限定:告知他要遵从非贪婪匹配 推荐使用!</span>
<span># print(re.findall(‘a.*?b‘, ‘ab a1b a*()b, aaaaaab‘)) # [‘ab‘, ‘a1b‘, ‘a*()b‘]</span>
<span># []: 括号中可以放任意一个字符,一个中括号代表一个字符</span>
<span># - 在[]中表示范围,如果想要匹配上- 那么这个-符号不能放在中间.</span>
<span># ^ 在[]中表示取反的意思.</span>
<span># print(re.findall(‘a.b‘, ‘a1b a3b aeb a*b arb a_b‘)) # [‘a1b‘, ‘a3b‘, ‘a4b‘, ‘a*b‘, ‘arb‘, ‘a_b‘]</span>
<span># print(re.findall(‘a[abc]b‘, ‘aab abb acb adb afb a_b‘)) # [‘aab‘, ‘abb‘, ‘acb‘]</span>
<span># print(re.findall(‘a[0-9]b‘, ‘a1b a3b aeb a*b arb a_b‘)) # [‘a1b‘, ‘a3b‘]</span>
<span># print(re.findall(‘a[a-z]b‘, ‘a1b a3b aeb a*b arb a_b‘)) # [‘aeb‘, ‘arb‘]</span>
<span># print(re.findall(‘a[a-zA-Z]b‘, ‘aAb aWb aeb a*b arb a_b‘)) # [‘aAb‘, ‘aWb‘, ‘aeb‘, ‘arb‘]</span>
<span># print(re.findall(‘a[0-9][0-9]b‘, ‘a11b a12b a34b a*b arb a_b‘)) # [‘a11b‘, ‘a12b‘, ‘a34b‘]</span>
<span># print(re.findall(‘a[*-+]b‘,‘a-b a*b a+b a/b a6b‘)) # [‘a*b‘, ‘a+b‘]</span>
<span># - 在[]中表示范围,如果想要匹配上- 那么这个-符号不能放在中间.</span>
<span># print(re.findall(‘a[-*+]b‘,‘a-b a*b a+b a/b a6b‘)) # [‘a-b‘, ‘a*b‘, ‘a+b‘]</span>
<span># print(re.findall(‘a[^a-z]b‘, ‘acb adb a3b a*b‘)) # [‘a3b‘, ‘a*b‘]</span>
分组
<span># () 制定一个规则,将满足规则的结果匹配出来</span>
<span># print(re.findall(‘(.*?)_sb‘, ‘alex_sb wusir_sb 日天_sb‘)) # [‘alex‘, ‘ wusir‘, ‘ 日天‘]</span>
<span># 应用举例:</span>
<span># print(re.findall(‘href="(.*?)"‘,‘<a href="http://www.baidu.com">点击</a>‘))#[‘http://www.baidu.com‘]</span>
或
<span># | 匹配 左边或者右边</span>
<span># print(re.findall(‘alex|太白|wusir‘, ‘alex太白wusiraleeeex太太白odlb‘)) # [‘alex‘, ‘太白‘, ‘wusir‘, ‘太白‘]</span>
<span># print(re.findall(‘compan(y|ies)‘,‘Too many companies have gone bankrupt, and the next one is my company‘)) # [‘ies‘, ‘y‘]</span>
<span># print(re.findall(‘compan(?:y|ies)‘,‘Too many companies have gone bankrupt, and the next one is my company‘)) # [‘companies‘, ‘company‘]</span>
<span># 分组() 中加入?: 表示将整体匹配出来而不只是()里面的内容。</span>
常用方法举例
<span>#1 findall 全部找到返回一个列表。</span>
<span># print(re.findall(‘a‘, ‘alexwusirbarryeval‘)) # [‘a‘, ‘a‘, ‘a‘]</span>
<span># 2 search 只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。</span>
<span># print(re.search(‘sb|alex‘, ‘alex sb sb barry 日天‘)) # <_sre.SRE_Match object; span=(0, 4), match=‘alex‘></span>
<span># print(re.search(‘alex‘, ‘alex sb sb barry 日天‘).group()) # alex</span>
<span># 3 match:None,同search,不过在字符串开始处进行匹配,完全可以用search+^代替match</span>
<span># print(re.match(‘barry‘, ‘barry alex wusir 日天‘)) # <_sre.SRE_Match object; span=(0, 5), match=‘barry‘></span>
<span># print(re.match(‘barry‘, ‘barry alex wusir 日天‘).group()) # barry</span>
<span># 4 split 分割 可按照任意分割符进行分割</span>
<span># print(re.split(‘[ ::,;;,]‘,‘alex wusir,日天,太白;女神;肖锋:吴超‘)) # [‘alex‘, ‘wusir‘, ‘日天‘, ‘太白‘, ‘女神‘, ‘肖锋‘, ‘吴超‘]</span>
<span># 5 sub 替换</span>
<span># print(re.sub(‘barry‘, ‘太白‘, ‘barry是最好的讲师,barry就是一个普通老师,请不要将barry当男神对待。‘))</span>
<span># 太白是最好的讲师,太白就是一个普通老师,请不要将太白当男神对待。</span>
<span># print(re.sub(‘barry‘, ‘太白‘, ‘barry是最好的讲师,barry就是一个普通老师,请不要将barry当男神对待。‘,2))</span>
<span># 太白是最好的讲师,太白就是一个普通老师,请不要将barry当男神对待。</span>
<span># print(re.sub(‘([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)‘, r‘\5\2\3\4\1‘, r‘alex is sb‘))</span>
<span># sb is alex</span>
<span># 6 重复使用</span>
<span># obj=re.compile(‘\d{2}‘)</span>
<span># print(obj.search(‘abc123eeee‘).group()) #12</span>
<span># print(obj.findall(‘abc123eeee‘)) #[‘12‘],重用了obj</span>
<span># import re</span>
<span># ret = re.finditer(‘\d‘, ‘ds3sy4784a‘) #finditer返回一个存放匹配结果的迭代器</span>
<span># print(ret) # <callable_iterator object at 0x10195f940></span>
<span># print(next(ret).group()) #查看第一个结果</span>
<span># print(next(ret).group()) #查看第二个结果</span>
<span># print([i.group() for i in ret]) #查看剩余的左右结果</span>
命名分组举例(了解)
<span># 命名分组匹配:</span>
<span>ret <span>=<span> re<span>.<span>search<span>(<span>"<(?P<tag_name>\w+)>\w+</(?P=tag_name)>"<span>,<span>"<h1>hello</h1>"<span>)</span></span></span></span></span></span></span></span></span></span>
<span># #还可以在分组中利用?<name>的形式给分组起名字</span>
<span># #获取的匹配结果可以直接用group(‘名字‘)拿到对应的值</span>
<span># print(ret.group(‘tag_name‘)) #结果 :h1</span>
<span># print(ret.group()) #结果 :<h1>hello</h1></span>
<span>#</span>
<span># ret = re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>")</span>
<span># #如果不给组起名字,也可以用\序号来找到对应的组,表示要找的内容和前面的组内容一致</span>
<span># #获取的匹配结果可以直接用group(序号)拿到对应的值</span>
<span># print(ret.group(1)) #结果: hello</span>
<span># print(ret.group()) #结果 :<h1>hello</h1></span>
相关推荐
杨德龙 2020-11-11
不要皱眉 2020-10-14
满地星辰 2020-09-16
梦的天空 2020-08-25
lrjnlp 2020-07-19
qidu 2020-07-05
flyingssky 2020-07-05
flyingssky 2020-06-27
RuoShangM 2020-06-17
天高任鸟飞 2020-06-13
Darklovy 2020-06-11
qidu 2020-06-08
Darklovy 2020-06-07
jyj00 2020-06-06
flyingssky 2020-06-04
山水沐光 2020-05-26
山水沐光 2020-05-25