I have a long regex with many alternations and I want to be able to replace each match from the regex with itself followed by a new line ('\n').
What is the most efficient way to do so with re.sub()?
Here is a simple example:
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)
I know this for loop will not work, I am just hoping to clarify what I am trying to solve here. Thanks in advance for any help. I may be missing something very straightforward.
s = re.sub(pattern, "\\g<0>\n", s).