Eri*_*son 51 python regex replace sed
假设我想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'
Run Code Online (Sandbox Code Playgroud)
如何在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'
Run Code Online (Sandbox Code Playgroud)
mac*_*mac 63
你需要逃避反斜杠:
p.sub('gray \\1', s)
Run Code Online (Sandbox Code Playgroud)
或者你可以像正在使用正则表达式一样使用原始字符串:
p.sub(r'gray \1', s)
Run Code Online (Sandbox Code Playgroud)
jus*_*ile 21
因为我正在寻找类似的答案; 但是想要在替换中使用命名组,我想我会为其他人添加代码:
p = re.compile(r'blue (?P<animal>dog|cat)')
p.sub(r'gray \g<animal>',s)
Run Code Online (Sandbox Code Playgroud)
试试这个:
p.sub('gray \g<1>',s)
Run Code Online (Sandbox Code Playgroud)
离题,对于编号捕获组:
#/usr/bin/env python
import re
re.sub(
pattern=r'(\d)(\w+)',
repl='word: \\2, digit: \\1',
string='1asdf'
)
Run Code Online (Sandbox Code Playgroud)
word: asdf, digit: 1
Python使用文字反斜杠和一个基于索引的索引来进行编号的捕获组替换,如本示例所示。因此\1
,输入为'\\1'
,引用第一个捕获组(\d)
和\2
第二个捕获组。