如何使用Python正则表达式通过捕获组进行替换?
假设我想把 the blue dog and blue cat wore blue hats
这句话改成 the gray dog and gray cat wore blue hats
。
如果用 sed
工具,我可以这样做:
$ echo 'the blue dog and blue cat wore blue hats' | sed 's/blue \(dog\|cat\)/gray \1/g'
那么我在 Python 里怎么做类似的替换呢?我试过:
>>> import re
>>> s = "the blue dog and blue cat wore blue hats"
>>> p = re.compile(r"blue (dog|cat)")
>>> p.sub('gray \1',s)
'the gray \x01 and gray \x01 wore blue hats'
4 个回答
29
题外话,关于编号捕获组:
#/usr/bin/env python
import re
re.sub(
pattern=r'(\d)(\w+)',
repl='word: \\2, digit: \\1',
string='1asdf'
)
word: asdf, digit: 1
在Python中,使用字面意义上的反斜杠,并且编号从1开始来进行编号捕获组的替换。举个例子,\1
,在输入时写成'\\1'
,它指的是第一个捕获组(\d)
,而\2
则指的是第二个捕获组。
42
我在寻找类似的答案时,想要在替换操作中使用命名组,所以我决定把代码分享给大家:
p = re.compile(r'blue (?P<animal>dog|cat)')
p.sub(r'gray \g<animal>',s)
112
你需要对你的反斜杠进行转义:
p.sub('gray \\1', s)
另外,你也可以像你之前对正则表达式那样使用原始字符串:
p.sub(r'gray \1', s)