我有一个很长的正则表达式,有很多交替,我希望能够用正则表达式中的每个匹配项替换它本身,后跟一个新行 ('\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)
我知道这个 for 循环不起作用,我只是希望澄清我在这里要解决的问题。在此先感谢您的帮助。我可能遗漏了一些非常简单的东西。
最佳答案
要用自身替换整个匹配项,您可以使用替换反向引用 \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)
参见 Python demo
关于Python 正则表达式将每个匹配项替换为自身加上一个新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46289193/