python中re模块的match、search、findall、sub方法使用

您所在的位置:网站首页 正则表达式refind python中re模块的match、search、findall、sub方法使用

python中re模块的match、search、findall、sub方法使用

2024-07-16 01:20| 来源: 网络整理| 查看: 265

正则表达式

正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。(来自百度)

正则表达式语法

(图片资料转自https://www.cnblogs.com/chengege/p/11190782.html) 在这里插入图片描述

re模块

re模块是python中内置的,支持正则匹配的模块。

match方法

match方法的语法为:re.match(pattern,string,[flags=0]) pattern表示要匹配的字符串,string表示原始字符串。flags为标志位,控制正则匹配格式,如忽略大小写。默认值是0,代表无特殊匹配。 需要注意的是,match方法在匹配时,是从头开始匹配的,如果是从中间截取一段字符,进行匹配,是匹配不到的。 下面一段代码:

# 原始字符串 old_str = '{"phone_num":"${not_exist_num}","pwd":"123456"}' # 要匹配的字符串中${not_exist_num} replace_str = '${not_exist_num}' # 要匹配的字符串 {"phone_num": replace_str1 = '{"phone_num":' # 使用match方法进行分别匹配两个字符 res = re.match(replace_str, old_str) res1 = re.match(replace_str1, old_str)

进行调试,可发现,匹配replace_str时,返回的是None, 匹配replace_str1时,返回的是Match对象。 在这里插入图片描述 可以通过match对象的group方法,获取匹配结果: 在这里插入图片描述

由上面测试结果可知,match的方法的特点:

match方法是从头开始匹配的,从中间截取字符串,是无法匹配到的。这也是match方法的局限性。match方法匹配不到结果时,返回的是None,匹配到结果时,返回的是match对象。match方法匹配到结果时,使用match对象的group方法,获取匹配结果。 search方法

search方法的语法为:re.search(pattern,string,[flags=0]) pattern 表示要匹配的正则表达式, string 表示要匹配的字符串, flags为标志位,用于控制正则表达式的匹配方式,如是否区分大小写,多行匹配等等,默认为0,代表无特殊匹配。 search方法与match方法有些类似。search是在整个原始字符串查找匹配,匹配到第一个之后,就不会再继续了。 举例:

import re # 原始字符串 old_str = '{"phone_num1":"${not_exist_num}", "phone_num2":"${not_exist_num}", "pwd":"123456"}' # 要查找字符串中的${not_exist_num},原始字符串中有两处, 因为字符串中有特殊字符,所以需要r转义,$在正则中有特殊含义,所以还需要使用\进行转义 replace_str = r'\${not_exist_num}' replace_str1 = '1111' # 使用search方法 res = re.search(replace_str, old_str) res1 = re.search(replace_str1, old_str) # 使用group()获取匹配结果 s = res.group()

匹配上则返回match对象,使用group方法获取匹配字符串,匹配不到返回None。(这一点与match一样) 在这里插入图片描述 由上面测试结果可知,search的方法的特点:

search方法是全字符串匹配的,匹配到第一个结果,即返回结果,不再继续。search方法匹配不到结果时,返回的是None,匹配到结果时,返回的是match对象。search方法匹配到结果时,使用match对象的group方法,获取匹配结果。 findall方法

findall方法的语法是:re.findall(pattern, string, flags=0) pattern 表示要匹配的正则表达式, string 表示要匹配的字符串, flags为标志位,用于控制正则表达式的匹配方式,如是否区分大小写,多行匹配等等,默认为0,代表无特殊匹配。

findall是查找字符串中所有可匹配的,并将匹配结果以列表的形式返回。如果匹配不到,则返回一个空列表。

import re # 原始字符串 old_str = '{"phone_num1":"${not_exist_num}", "phone_num2":"${not_exist_num}", "pwd":"123456"}' # 要查找字符串中的${not_exist_num},原始字符串中有两处, 因为字符串中有特殊字符,所以需要r转义,$在正则中有特殊含义,所以还需要使用\进行转义 replace_str = r'\${not_exist_num}' replace_str1 = '1111' # 使用findall方法 res = re.findall(replace_str, old_str) res1 = re.findall(replace_str1, old_str)

在这里插入图片描述

sub方法

sub方法的格式为:re.sub(pattern, repl, string[, count]) pattern为匹配字符,可以是一串字符,也可以是正则表达式,repl要替换的字符串, string为原始字符串, count为替换次数,默认为0。 sub方法使用repl替换string中每一个匹配的子串后返回替换后的字符串。

比如下面一段代码,使用sub实现替换:

import re # 原始字符串 old_str = '{"phone_num1":"${not_exist_num}", "phone_num2":"${not_exist_num}", "pwd":"123456"}' # 要查找字符串中的${not_exist_num},原始字符串中有两处, 因为字符串中有特殊字符,所以需要r转义,$在正则中有特殊含义,所以还需要使用\进行转义 replace_str = r'\${not_exist_num}' replace_str1 = '111' # 使用sub方法 resu = re.sub(replace_str, "138098776754", old_str) # count 默认为0 res = re.sub(replace_str, "13544567865", old_str, count=1) res1 = re.sub(replace_str, "13489778966", old_str, count=2) res2 = re.sub(replace_str1, '222', old_str)

通过调试可知:

count默认为0时,会默认替换全部,指定count值时,则按照指定次数替换。可匹配到时,则返回匹配到的字符串。无法匹配到时,则返回原始的字符串。 0 上面这段代码,是直接替换的字符串,还可以通过正则表达式进行替换: import re # 原始字符串 old_str = 'a123b' # 要替换字符串中的数字,在正则表达式中,\d代表0-9 , 使用r进行转义 replace_str = r'\d' # 使用sub方法 res = re.sub(replace_str, "o", old_str) pass

上面代码进行调试后,原始字符串中的123,均被替换为o: 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3