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

标签 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)

我知道这个 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/

相关文章:

python - 无法在 SocketServer.TCPServer 中重用套接字

python - 无法使用 PyScript 访问文档

regex - 在 bash 中声明一个 awk 函数

python - 遍历嵌套列表时,我不断收到 "ValueError: list.remove(x): x not in list"

python - 使用 Pandas 创建 NumPy 数组

javascript - 正则表达式选择日期分隔符

regex - Elasticsearch-匹配十六进制的固定位数

c++ - QRegExp 麻烦匹配模式

python - 将 Python 变量传递给批处理文件

regex - 正则表达式匹配文本中带或不带逗号和小数的数字