Python中re模块的使用
编译正则表达式,返回一个正则表达式对象,该对象可复用。
re.compile(pattern, flags=0) # pattern:编译时用的表达式字符串 # flags:编译标志位,用于修改正则表达式匹配方式
常用flags
标志 | 含义 |
---|---|
re.S(DOTALL) |
使 . 匹配包括换行符在内的所有字符 |
re.I(IGNORECASE) |
忽略大小写 |
re.L(LOCALE) |
本地化识别匹配,影响 \w, \W, \b, \B, \s, \S |
re.M(MULTILINE) |
多行匹配,影响^ 和$ |
re.X(VERBOSE) |
详细模式,该模式下正则表达式可以是多行,忽略空白字符,且可以加注释 |
re.U |
根据Unicode 字符集解析字符,影响\w, \W, \b, \B, \d, \D, \s, \S |
match()
判断目标字符串是否在字符串开始处匹配。
re.match(pattern, string, flags=0) # pattern:正则表达式对象 # string:目标字符串
用法:
re.match('hel', 'hello world').group() # 'hel' re.match('hel', 'HELLO WORLD', re.I).group() # 'HEL'
注意:不完全匹配,即只判断字符串开头是否匹配。可以在正则表达式对象末尾加上边界符$
来实现完全匹配。
search()
re.search(pattern, string, flags=0) # pattern:正则表达式对象 # string:目标字符串
用法:
re.search('wor', 'hello world').group() # wor re.search('wor', 'HELLO WORLD', re.I).group() # WOR
match() VS search()
相同:
-
一旦匹配成功,会返回一个
match object
对象,该对象包含group
等方法。
re.search('wor', 'hello world').start() # 6 # 返回匹配开始处索引 re.search('wor', 'hello world').end() # 9 # 返回匹配结束处索引 re.search('wor', 'hello world').span() # (6, 9) # 返回匹配开始、结束处索引组成的元组 re.search('wor', 'hello world').group() # 'wor' # 返回整体匹配的字符串,可以一次输入多个组号(正则表达式对象需要有进行分组)
不同:
-
match
从字符串开始处匹配,开始处不匹配则匹配失败;search
在字符串整体中进行匹配,只要找到一个匹配对象即返回匹配字符串。
findall()
遍历匹配,返回所有匹配的字符串组成的列表。
re.findall(pattern, string, flags=0) # pattern:正则表达式对象 # string:目标字符串
用法:
re.findall('o', 'hello world') # ['o', 'o']
finditer()
遍历匹配,返回一个匹配结果的迭代器。
re.finditer(pattern, string, flags=0) # pattern:正则表达式对象 # string:目标字符串
用法:
res = re.finditer('o', 'hello world') for i in res: print(i.group()) # o o
re.finditer()
函数返回结果是一个迭代器,迭代对象为match object
对象。
split()
根据匹配的字符串分割目标字符串,返回分割后结果列表。
re.split(pattern, string[, maxsplit[, flags=0]]) # pattern:正则表达式对象 # string:目标字符串 # maxsplit:最大分割次数,默认为无穷大即分割为最小块
用法:
re.split('o', 'hello world') # ['hell', 'w', 'rld'] re.split('o', 'hello world', 1) # ['hell', 'world']
sub()
替换给定字符串的匹配子串,返回替换后对象。
re.sub(pattern, repl, string, count) # pattern:正则表达式对象 # repl:替换对象 # string:目标字符串 # count:替换次数,默认无穷大即全部替换
用法:
re.sub('o', 'p', 'hello world') # hellp world re.sub('o', 'p', 'hello world', 1) # hellp wprld
subn()
返回替换后结果和次数组成的元组。
re.subn(pattern, repl, string, count=0, flags=0) # pattern:正则表达式对象 # repl:替换对象 # string:目标字符串 # count:替换次数,默认全部替换
用法:
re.subn('o','p', 'hello world') # ('hellp wprld', 2) re.subn('o','p', 'hello world', 1) # ('hellp world', 1) re.subn('o','p', 'hello world', 9) # ('hellp wprld', 2)
Python
支持的正则表达元字符和语法
语法 | 说明 | 表达式实例 | 完整匹配的字符串 |
---|---|---|---|
一般字符 | 匹配自身 | abc |
abc |
. |
匹配除换行符\n 之外所有字符,DOTALL 模式也能匹配换行符 |
a.c |
agc |
[...] |
字符集。对应位置可以为字符集中任意字符,[] 内可以是具体字符也可以是字符范围,如[abc] 或[a-c] 。第一个字符如果是^ 则表示取反。 |
a[bcd]e |
abe |
\d |
数字:[0-9] |
a\dc |
a2c |
\D |
非数字:[^\d] |
a\Dc |
abc |
\s |
空白字符:[<空格>\t\r\n\f\v] |
a\sc |
a c |
\S |
非空白字符:[^\s] |
a\Sc |
abc |
\w |
单词字符:[A-Za-z0-9] |
a\wc |
aBc |
\W |
非单词字符:[^\w] |
a\Wc |
a c |
* |
匹配前一个字符0 次或多次 |
abc* |
ab,abccc |
+ |
匹配前一个字符1 次或多次 |
abc+ |
abc,abccc |
? |
匹配前一个字符0 次或1 次 |
abc? |
ab,abc |
{m} |
匹配前一个字符m 次 |
ab{2}c |
abbc |
{m, n} |
匹配前一个字符m 到n 次 |
ab{1,2}c |
abc,abbc |
^ |
匹配字符串开头,多行模式中匹配每一行开头 | ^abc |
abc |
$ |
匹配字符串末尾,多行模式中匹配每一行末尾 | abc$ |
abc |
\A |
仅匹配字符串开头 | \Abc |
abc |
\Z |
仅匹配字符串末尾 | abc\Z |
abc |
\b |
匹配\w 和\W 之间 |
a\b!ba |
a!bc |
\B |
[^\b] |
a\Bbc |
abc |
| |
或操作符 | abc|def |
abc,def |
(...) |
括号内表达式作为分组,从字符串起始开始匹配,每匹配到一个符合条件的则编号+1 。分组表达式作为一个整体,可以后接数量词。 |
(abc){2},a(123|456)c |
abcabc,a456c |
(?P<name>...) |
分组,除原有编号之外再指定一个别名。 | (?P<id>abc){2} |
abcabc |
<number> |
引用编号为<number> 的分组匹配到的字符串 |
(\d)abc1 |
1abc1 |
(?P=name) |
引用别名为<name> 的分组匹配到的字符串 |
(?P<id>\d)abc(?P=id) |
1abc1,3abc3 |
(?:...) |
(...) 的不分组版本,用于使用或操作符后接数量词 |
(?:abc){2} |
abcabc |
(?#...) |
# 后内容作为注释被忽略 |
abc(#coment)1 |
abc1 |
(?=...) |
之后的字符串内容需要匹配表达式才能成功匹配,不消耗字符串内容 | a(?=\d) |
后面是数字的a |
(?!...) |
a(?!\d) |
后面不是数字的a |
|
(?<=...) |
a(<=\d) |
前面是数字的a |
|
(?<!...) |
a(<!\d) |
前面是不是数字的a |
贪婪模式和非贪婪模式
使用正则表达式进行模式匹配的时候默认都是贪婪模式,即在匹配范围内尽可能匹配最大长度的字符串。贪婪模式会影响到*
、+
、{m, n}
等元字符的使用,可以在后面加上?
使其变为非贪婪模式。
re.search('abc.*d', 'abcdccccd') # 贪婪模式,匹配整个字符串,输出 'abcdccccd' re.search('abc.*?d', 'abcdccccd') # 非贪婪模式,匹配到 abc 后面一个 d 即停止匹配,输出 'abcd'