如何使用 pythonregex 替换使用捕获的组?

假设我想把 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'
73875 次浏览

You need to escape your backslash:

p.sub('gray \\1', s)

alternatively you can use a raw string as you already did for the regex:

p.sub(r'gray \1', s)

Try this:

p.sub('gray \g<1>',s)

As I was looking for a similar answer; but wanting using named groups within the replace, I thought I'd add the code for others:

p = re.compile(r'blue (?P<animal>dog|cat)')
p.sub(r'gray \g<animal>',s)

Off topic, For numbered capture groups:

#/usr/bin/env python
import re


re.sub(
pattern=r'(\d)(\w+)',
repl='word: \\2, digit: \\1',
string='1asdf'
)

word: asdf, digit: 1

Python uses literal backslash, plus one-based-index to do numbered capture group replacements, as shown in this example. So \1, entered as '\\1', references the first capture group (\d), and \2 the second captured group.