当前位置:生活全书馆 >

学习教育

> Python常用的正则表达式处理函数详解

Python常用的正则表达式处理函数详解

正则表达式是一个特殊的字符序列,用于简洁表达一组字符串特征,检查一个字符串是否与某种模式匹配,使用起来十分方便。

在Python中,我们通过调用re库来使用re模块:

import re

下面介绍Python常用的正则表达式处理函数。

re.match函数

re.match 函数从字符串的起始位置匹配正则表达式,返回match对象,如果不是起始位置匹配成功的话,match()就返回None。

re.match(pattern, string, flags=0)

pattern:匹配的正则表达式。

string:待匹配的字符串。

flags:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。具体参数为:

re.I:忽略大小写。

re.L:表示特殊字符集 w, W, , B, s, S 依赖于当前环境。

re.M:多行模式。

re.S:即 . ,并且包括换行符在内的任意字符(. 不包括换行符)。

re.U:表示特殊字符集 w, W, , B, d, D, s, S 依赖于 Unicode 字符属性数据库。

re.X:为了增加可读性,忽略空格和 # 后面的注释。

import re#从起始位置匹配r1=re.match('abc','abcdefghi')print(r1)#不从起始位置匹配r2=re.match('def','abcdefghi')print(r2)

运行结果:

Python常用的正则表达式处理函数详解

其中,span表示匹配成功的整个子串的索引。

使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。

group(num):匹配的整个表达式的字符串,group() 可以一次输入多个组号,这时它将返回一个包含那些组所对应值的元组。

groups():返回一个包含所有小组字符串的元组,从 1 到 所含的小组号。

import res='This is a demo'r1=re.match(r'(.*) is (.*)',s)r2=re.match(r'(.*) is (.*?)',s)print(r1.group())print(r1.group(1))print(r1.group(2))print(r1.groups())print()print(r2.group())print(r2.group(1))print(r2.group(2))print(r2.groups())

运行结果:

Python常用的正则表达式处理函数详解 第2张

上述代码中的(.*)和(.*?)表示正则表达式的贪婪匹配与非贪婪匹配。

re.search函数

re.search函数扫描整个字符串并返回第一个成功的匹配,如果匹配成功则返回match对象,否则返回None。

re.search(pattern, string, flags=0)

pattern:匹配的正则表达式。

string:待匹配的字符串。

flags:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。

import re#从起始位置匹配r1=re.search('abc','abcdefghi')print(r1)#不从起始位置匹配r2=re.search('def','abcdefghi')print(r2)

运行结果:

Python常用的正则表达式处理函数详解 第3张

使用group(num) 或 groups() 匹配对象函数来获取匹配表达式。

group(num=0):匹配的整个表达式的字符串,group() 可以一次输入多个组号,这时它将返回一个包含那些组所对应值的元组。

groups():返回一个包含所有小组字符串的元组,从 1 到 所含的小组号。

import res='This is a demo'r1=re.search(r'(.*) is (.*)',s)r2=re.search(r'(.*) is (.*?)',s)print(r1.group())print(r1.group(1))print(r1.group(2))print(r1.groups())print()print(r2.group())print(r2.group(1))print(r2.group(2))print(r2.groups())

运行结果:

Python常用的正则表达式处理函数详解 第4张


从上面不难发现re.match与re.search的区别:re.match只匹配字符串的起始位置,只要起始位置不符合正则表达式就匹配失败,而re.search是匹配整个字符串,直到找到一个匹配为止。

re.compile 函数

compile 函数用于编译正则表达式,生成一个正则表达式对象,供 match() 和 search() 这两个函数使用。

re.compile(pattern[, flags])

pattern:一个字符串形式的正则表达式。

flags:可选,表示匹配模式,比如忽略大小写,多行模式等。

import re#匹配数字r=re.compile(r'd+') r1=r.match('This is a demo')r2=r.match('This is 111 and That is 222',0,27)r3=r.match('This is 111 and That is 222',8,27) print(r1)print(r2)print(r3)

运行结果:

Python常用的正则表达式处理函数详解 第5张

findall函数

搜索字符串,以列表形式返回正则表达式匹配的所有子串,如果没有找到匹配的,则返回空列表。

需要注意的是,match 和 search 是匹配一次,而findall 匹配所有。

findall(string[, pos[, endpos]])

string:待匹配的字符串。

pos:可选参数,指定字符串的起始位置,默认为0。

endpos:可选参数,指定字符串的结束位置,默认为字符串的长度。

import re#匹配数字r=re.compile(r'd+') r1=r.findall('This is a demo')r2=r.findall('This is 111 and That is 222',0,11)r3=r.findall('This is 111 and That is 222',0,27) print(r1)print(r2)print(r3)

运行结果:

Python常用的正则表达式处理函数详解 第6张

re.finditer函数

和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并把它们作为一个迭代器返回。

re.finditer(pattern, string, flags=0)

pattern:匹配的正则表达式。

string:待匹配的字符串。

flags:标志位,用于控制正则表达式的匹配方式,如是否区分大小写,多行匹配等。

import re r=re.finditer(r'd+','This is 111 and That is 222')for i in r:  print (i.group())

运行结果:

Python常用的正则表达式处理函数详解 第7张

re.split函数

将一个字符串按照正则表达式匹配的子串进行分割后,以列表形式返回。

re.split(pattern, string[, maxsplit=0, flags=0])

pattern:匹配的正则表达式。

string:待匹配的字符串。

maxsplit:分割次数,maxsplit=1分割一次,默认为0,不限次数。

flags:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等。

import re r1=re.split('W+','This is 111 and That is 222') r2=re.split('W+','This is 111 and That is 222',maxsplit=1) r3=re.split('d+','This is 111 and That is 222') r4=re.split('d+','This is 111 and That is 222',maxsplit=1) print(r1)print(r2)print(r3)print(r4)

运行结果:

Python常用的正则表达式处理函数详解 第8张

re.sub函数

re.sub函数用于替换字符串中的匹配项。

re.sub(pattern, repl, string, count=0, flags=0)

pattern:正则中的模式字符串。

repl:替换的字符串,也可为一个函数。

string:要被查找替换的原始字符串。

count:模式匹配后替换的最大次数,默认0表示替换所有的匹配。

import re r='This is 111 and That is 222'# 删除字符串中的数字r1=re.sub(r'd+','',r)print(r1)# 删除非数字的字符串 r2=re.sub(r'D','',r)print(r2)

运行结果:

Python常用的正则表达式处理函数详解 第9张

到此这篇关于Python常用的正则表达式处理函数详解的文章就介绍到这了,希望大家以后多多支持好二三四!

<link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea273683c8.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea303194c0eaf695827b59325e.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea2c3096c5e3db978c6e492c.js"></script> <script>SyntaxHighlighter.autoloader( 'applescript https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f3809476490d49a2a6da28.js', 'actionscript3 as3 https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f3a3d7.js', 'bash shell https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f0919772.js', 'coldfusion cf https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f19f887e6a2b59b9a0c4.js', 'cpp c https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f18094.js', 'obj-c objc https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7fd928e59.js', 'c# c-sharp csharp https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f1a38c7b5e2e.js', 'css https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f18397.js', 'delphi pascal https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f695886a4437.js', 'diff patch pas https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f699827c.js', 'erl erlang https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f782887b4239.js', 'groovy https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f5828b755a27.js', 'haxe hx https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7fa919c7f.js', 'java https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f891927b.js', 'jfx javafx https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f891927b6a06.js', 'js jscript javascript https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f8a38768452e5e.js', 'perl pl https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e2959676.js', 'php https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e29894.js', 'text plain https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e29c857342.js', 'py python https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e28990724330.js', 'ruby rails ror rb https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e0858663.js', 'scala https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e19385764d.js', 'sql https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e18188.js', 'vb vbnet https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e492.js', 'ps powershell https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7e29f937f5e0d42b5a3c6.js', 'xml xhtml xslt html https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7ea9d88.js', 'go golang https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4e1163081d9fc80c0d628/a4ea262b84dee7f59f.js' );</script> <script type="text/javascript"> SyntaxHighlighter.all(); </script>

  • 文章版权属于文章作者所有,转载请注明 https://shqsg.com/zh/xuexijiaoyu/552e0w.html