Python 正则表达式re模块的使用
基本上所有的编程语言都会有正则表达式,正则表达式是用来匹配一段字符串的表达式。
在Python中需要通过正则表达式对字符串进行匹配的时候,可以使用内置模块re。
一、re中常用字符的含义
re模块中的字符是非常多的,我们例举如下常用的。
二、match和search的用法区别
match_result = re.match(r"read", "We read the world wrong and say that it deceives us.")print(match_result)# print(match_result.group())search_result = re.search(r"read", "We read the world wrong and say that it deceives us.")print(search_result)print(search_result.group())
运行结果:
None<re.Match object; span=(3, 7), match='read'>read
1.match是从字符串的开头开始匹配,而search可以从字符串的任意位置开始匹配。
2.不管是match还是search,匹配成功后都是返回一个re.Match对象,里面包含了匹配结果在字符串中的索引范围及匹配结果。如果没有匹配到结果,则返回None。
3.当匹配到结果后,调用re.Match对象的group()方法,可以返回匹配的结果。如果没有匹配到结果,调用group()方法会报错。
三、findall匹配所有结果
findall_result = re.findall(r'\d+', 'We read the world wrong 777 and 2 say that it deceives 007 us.')print(findall_result)
运行结果:
['777', '2', '007']
使用findall()方法,会依次匹配字符串中所有满足条件的结果,返回一个列表,如果没有匹配到结果,则返回的是一个空列表。
四、sub对匹配结果进行替换
语法:re.sub(正则表达式,替换内容,字符串)
sub_result = re.sub(r'a', 'A', 'We read the world wrong and say that it deceives us.')print(sub_result)
运行结果:
We reAd the world wrong And sAy thAt it deceives us.
使用sub()方法,可以对字符串中匹配到的字符进行替换,sub()会从头到尾匹配所有满足正则表达式的结果,然后都进行替换,返回替换后的字符串。结果与str.replace()方法的结果相同。
如果没有匹配到结果,则不做替换。
五、贪婪模式和非贪婪模式
result1 = re.search(r'\d+', 'We read the world wrong 7777777 and 2 say that it deceives 007 us.')print(result1.group())result2 = re.search(r'\d+?', 'We read the world wrong 7777777 and 2 say that it deceives 007 us.')print(result2.group())
运行结果:
77777777
上面的代码中,使用\d+会匹配所有所有的7,使用\d+?则只匹配一个7.
在Python中,re默认是贪婪的,即在满足正则表达式的情况下,总是尝试匹配尽可能多的字符;
非贪婪则相反,总是尝试匹配尽可能少的字符。
在"*","?","+","{m,n}"后面加上?,可以使贪婪模式变成非贪婪模式。