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() 返回一个元组包含匹配 (开始,结束) 的位置