fork(4) download
  1. import re
  2. matches = []
  3. def repl(m):
  4. matches.append(m.group())
  5. return "{}\n".format(m.group())
  6.  
  7. 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?'
  8. pattern = re.compile(r'words[,]|sentence[,]|problem[.]')
  9. s = re.sub(pattern, repl, s)
  10.  
  11. print(s)
  12. print(matches)
Success #stdin #stdout 0.01s 23304KB
stdin
Standard input is empty
stdout
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?
['words,', 'sentence,', 'problem.']