Python正则表达式将每个匹配替换为自身加上新行

rah*_*f23 4 python regex

我有一个很长的正则表达式,有很多交替,我希望能够将正则表达式中的每个匹配替换为自身,后跟新行('\n')。

使用 re.sub() 最有效的方法是什么?

这是一个简单的例子:

s = 'I want to be able to replace many words, especially in this sentence, since it will help me solve by problem. That makes sense right?'

pattern = re.compile(r'words[,]|sentence[,]|problem[.]')

for match in matches:
    re.sub(pattern, match + '\n', match)
Run Code Online (Sandbox Code Playgroud)

我知道这个 for 循环不起作用,我只是希望澄清我想在这里解决的问题。预先感谢您的任何帮助。我可能会错过一些非常简单的东西。

Wik*_*żew 5

要将整个匹配替换为自身,您可以使用替换反向引用\g<0>。但是,您希望替换并将匹配项存储在变量中。您需要将回调方法作为替换参数传递给re.sub,并返回整个匹配值 ( match.group()) 并在该值后附加换行符:

import re
matches = []                          # Variable to hold the matches
def repl(m):                          # m is a match data object
    matches.append(m.group())         # Add a whole match value
    return "{}\n".format(m.group())   # Return the match and a newline appended to it

s = 'I want to be able to replace many words, especially in this sentence, since it will help me solve by problem. That makes sense right?'
pattern = re.compile(r'words[,]|sentence[,]|problem[.]')
s = re.sub(pattern, repl, s)

print(s)
print(matches)
Run Code Online (Sandbox Code Playgroud)

查看Python 演示