zl程序教程

您现在的位置是:首页 >  前端

当前栏目

正则表达式高级用法

正则表达式 用法 高级
2023-06-13 09:12:14 时间

一、正则高级用法

1、分组

概念

处理简单的判断是否匹配之外,正则表达式还有提取子串的功能,用()表示的就是要提取的分组

代码

<span class="hljs-keyword">import</span> re

ret = re.search(<span class="hljs-string">r"(?P<phone>(?P<quhao>0\d{2,3})-(\d{7,8}))"</span>, <span class="hljs-string">"zutuanxue_com's phone is 010-88888888"</span>)
print(ret)
<span class="hljs-comment"># 组的排序:从外到内,从左到右</span>
print(ret.group(<span class="hljs-number">0</span>)) <span class="hljs-comment">#组中下标为0的表示原数据</span>
print(ret.group(<span class="hljs-string">"phone"</span>))
print(ret.group(<span class="hljs-number">1</span>))
print(ret.group(<span class="hljs-string">"quhao"</span>)) <span class="hljs-comment"># 可以根据组的名字获取组的数据</span>
print(ret.group(<span class="hljs-number">2</span>))
print(ret.group(<span class="hljs-number">3</span>))
<span class="hljs-comment">#查看匹配的各组的数据</span>
print(ret.groups())

说明

  • 正则表达式中定义了组,就可以在Match对象上用group()方法提取出子串来
  • group(0)永远是原始字符串,group(1)、group(2)……表示第1、2、……个子串

2、编译

概念

当在python中使用正则表达式时,re模块会做两件事,一件是编译正则表达式,如果表达式的字符串本身不合法,会报错。另一件是用编译好的正则表达式提取匹配字符串

编译优点

如果一个正则表达式要使用几千遍,每一次都会编译,出于效率的考虑进行正则表达式的编译,就不需要每次都编译了,节省了编译的时间,从而提升效率

compile()函数

原型

def compile(pattern, flags=0)

作用

将pattern模式编译成正则对象

参数

参数

说明

pattern

匹配的正则表达式(一种字符串的模式)

flags

标识位,用于控制正则表达式的匹配方式

flags

说明

re.I

是匹配对大小写不敏感

re.L

做本地化识别匹配

re.M

多行匹配,影响到^和$

re.S

使.匹配包括换行符在内的所有字符

re.U

根据Unicode字符集解析字符,影响\w、\W、\b、\B

re.X

通过给予我们功能灵活的格式以便更好的理解正则表达式

返回值

编译好的正则对象

示例

<span class="hljs-keyword">import</span> re

re_phone = re.compile(<span class="hljs-string">r"(0\d{2,3}-\d{7,8})"</span>)
print(re_phone, type(re_phone))

编译后其他方法的使用

原型

def match(self, string, pos=0, endpos=-1)
def search(self, string, pos=0, endpos=-1)
def findall(self, string, pos=0, endpos=-1)
def finditer(self, string, pos=0, endpos=-1)

参数

参数

说明

string

待匹配的字符串

pos

从string字符串pos下标开始

endpos

结束下标

示例

s1 = <span class="hljs-string">"zutuanxue_com's phone is 010-88888888"</span>
s2 = <span class="hljs-string">"kaige's phone is 010-99999999"</span>
ret1 = re_phone.search(s1)
print(ret1, ret1.group(<span class="hljs-number">1</span>))
ret2 = re_phone.search(s2)
print(ret2, ret2.group(<span class="hljs-number">1</span>))

3、贪婪匹配

贪婪概念

匹配尽可能多的字符

<span class="hljs-keyword">import</span> re

print(re.findall(<span class="hljs-string">r"(\d+0)"</span>, <span class="hljs-string">"12340567085465046567"</span>))

非贪婪概念

尽可能少的匹配称为非贪婪匹配,*?、+?即可

<span class="hljs-keyword">import</span> re

print(re.findall(<span class="hljs-string">r"(\d+?0)"</span>, <span class="hljs-string">"12340567085465046567"</span>))

常见示例

/* part1 */ /* part2 */

<span class="hljs-keyword">import</span> re

s = <span class="hljs-string">'''/* zutuanxue_com good */ dsdgergaergser /* kaige nice */'''</span>
print(re.findall(<span class="hljs-string">r"/\* (.*) \*/"</span>, s))
print(re.findall(<span class="hljs-string">r"/\* (.*?) \*/"</span>, s))

二、高级使用

1、字符串切割

早期字符串切割代码

s1 = <span class="hljs-string">"zutuanxue_com is a good man"</span>
print(s1.split(<span class="hljs-string">" "</span>))

现在的需求

字符串"zutuanxue_com is a good man"中的单词切割出来(注意字符串中空格个数不定)

正则中的split()函数

原型:re.split(pattern, string)

作用:切割字符串

解决需求

<span class="hljs-keyword">import</span> re

s2 = <span class="hljs-string">"zutuanxue_com     is  a good    man"</span>
print(re.split(<span class="hljs-string">r"\s+"</span>, s2))

切割"zutuanxue_com#is$a%good&man"中的单词

<span class="hljs-keyword">import</span> re

s3 = <span class="hljs-string">"zutuanxue_com#is$a%good&man"</span>
print(re.split(<span class="hljs-string">r"[#$%&]"</span>, s3))

2、字符串替换

早期字符串替换代码

s1 = <span class="hljs-string">"zutuanxue_com is a good man"</span>
s2 = s1.replace(<span class="hljs-string">"good"</span>, <span class="hljs-string">"nice"</span>)
print(s2)

现在的需求

将字符串"zutuanxue_com is a good man!zutuanxue_com is a nice man!zutuanxue_com is a cool man"中的"good"、“nice”、“cool"都改为"handsome”

sub函数与subn()函数

原型

def sub(pattern, repl, string, count=0, flags=0)
def subn(pattern, repl, string, count=0, flags=0)

作用

在目标字符串string中查找匹配的pattern模式的字符串,再把它们替换成指定repl字符串,可以指定最多替换count次,否则替换所有

区别

a、sub返回一个替代后的字符串

b、subn返回一个元组,元组的第一个元素为替换后的字符串,第二个元素为替换的次数

解决需求

<span class="hljs-keyword">import</span> re

s3 = <span class="hljs-string">"zutuanxue_com is a good man!zutuanxue_com is a nice man!zutuanxue_com is a cool man"</span>
print(re.sub(<span class="hljs-string">r"good|nice|cool"</span>, <span class="hljs-string">"handsome"</span>, s3))
print(re.subn(<span class="hljs-string">r"good|nice|cool"</span>, <span class="hljs-string">"handsome"</span>, s3))