Python 正则用法总结

re模块含有的方法,主要是查找匹配和查找替换两大类

查找匹配

match(pattern, string, flags=0)
参数描述
pattern正则表达式
string要匹配的字符串
flags标志位

从字符串的头开始匹配,成功的话返回Match对象,失败返回None

>>> print(re.match('hel', 'hello world'))
<re.Match object; span=(0, 3), match='hel'>
>>> print(re.match('el', 'hello world'))
None
fullmatch(pattern, string, flags=0)
参数描述
pattern正则表达式
string要匹配的字符串
flags标志位

这个方法似乎没有啥用,直接比较好了

>>> print(re.fullmatch('hello', 'hello world'))
None
>>> print(re.fullmatch('hello world', 'hello world'))
<re.Match object; span=(0, 11), match='hello world'>
search(pattern, string, flags=0)
参数描述
pattern正则表达式
string要匹配的字符串
flags标志位

全字符串查找,有匹配的返回第一个Match对象,没有匹配返回None

>>> print(re.search('wor', 'hello world'))
<re.Match object; span=(6, 9), match='wor'>

findall(pattern, string, flags=0)

参数描述
pattern正则表达式
string要匹配的字符串
flags标志位

在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表

>>> print(re.findall('wor', 'hello world'))
['wor']
>>> print(re.findall('l', 'hello world'))
['l', 'l', 'l']
>>> print(re.findall('q', 'hello world'))
[]

finditer(pattern, string, flags=0)

参数描述
pattern正则表达式
string要匹配的字符串
flags标志位

在字符串中找到正则表达式所匹配的所有子串,返回一个Match对象的迭代器

>>> a = re.finditer('l', 'hello world')
>>> a
<callable_iterator object at 0x10f0d8c90>
>>> for i in a:
...     print(i)
... 
<re.Match object; span=(2, 3), match='l'>
<re.Match object; span=(3, 4), match='l'>
<re.Match object; span=(9, 10), match='l'>

compile(pattern, flags=0)

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

>>> r = re.compile(r'\d')
>>> print(r.search('sfsfe5slf'))
<re.Match object; span=(5, 6), match='5'>
>>> print(r.match('3sfsfeslf'))
<re.Match object; span=(0, 1), match='3'>

查找替换

sub(pattern, repl, string, count=0, flags=0)
参数描述
pattern正则表达式
repl替换的字符串,也可为一个函数
string要匹配的字符串
count模式匹配后替换的最大次数,默认 0 表示替换所有的匹配
flags标志位

替换字符串中的匹配项,并返回此字符串

>>> print(re.sub('l', '', 'hello world'))
heo word
>>> print(re.sub(r'\?.*$', '', 'hello world?xxxx')) 
hello world

#repl如果是一个方法,每一个被匹配到的子字符串作为参数执行替换函数
>>> def test(v):
    		return str(int(v.group()) * 2)
>>> print(re.sub(r'\d', test,  '1 hello 23'))
2 hello 46
subn(pattern, repl, string, count=0, flags=0)
参数描述
pattern正则表达式
repl替换的字符串,也可为一个函数
string要匹配的字符串
count模式匹配后替换的最大次数,默认 0 表示替换所有的匹配
flags标志位

替换字符串中的匹配项,并返回 (new_string, number),new_string是替换后的字符串,number是替换数量

>>> def test(v):
    		return str(int(v.group()) * 2)
>>> print(re.subn(r'\d', test,  '1 hello 23'))
('2 hello 46', 3)

split(pattern, string, maxsplit=0, flags=0)

参数描述
pattern正则表达式
string要匹配的字符串
maxsplit分隔次数,maxsplit=1 分隔一次,默认为 0,不限制次数
flags标志位

按照能够匹配的子串将字符串分割后返回列表

>>> print(re.split(r' ', '1 hello 2'))
['1', 'hello', '2']
>>> print(re.split(r' ', '1 hello 2', 1))
['1', 'hello 2']
>>> print(re.split(r'a', '1 hello 2', 1)) # 匹配不上就不分
['1 hello 2']

flags可选参数

多个标志可以通过按位 OR(|) 它们来指定。如 re.I | re.M 被设置成 I 和 M 标志

参数描述
re.I忽略大小写
re.L表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境
re.M多行模式
re.S即为 . 并且包括换行符在内的任意字符(. 不包括换行符)
re.U表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库
re.X为了增加可读性,忽略空格和 # 后面的注释

Match对象的方法

  • group() 返回匹配的字符串
  • start() 返回匹配开始的位置
  • end() 返回匹配结束的位置
  • span() 返回一个元组包含匹配 (开始,结束) 的位置
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值