我有一个很长的正则表达式,有许多替换,我希望能够用正则表达式替换正则表达式中的每个匹配,后跟一个新行(' \ 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循环不起作用,我只是希望澄清我在这里要解决的问题。在此先感谢您的帮助。我可能会遗漏一些非常简单的事情。
答案 0 :(得分:1)
要替换整个匹配项,您可以使用替换反向引用\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
答案 1 :(得分:0)
就像这样?
text ='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?'
text_list = tex t.replace('.',',').strip(',|.|?').split(',')
##Remove the beginning and end symbols.And split by ','
print (text_list)
for i in text_list:
ii=i.split(',')
print(ii)
结果
['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']
['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']
答案 2 :(得分:0)
re.sub 的第二个参数可以是一个字符串,也可以是一个接受匹配实例并返回一个字符串的可调用对象。这样做
def break_line(match):
return "\n" + match.group()
text = re.sub(pattern, break_line, text)