fork download
  1. import re
  2.  
  3. s = 'F6[ab]\nF3[ab]'
  4. n = 'F6[ab]' # <= want to keep this in my string but want to change 'ab' in the new line
  5. k = 'ab' # <= do not want to keep this in my string
  6. c = '1' # <= change 'ab' -> '1'
  7.  
  8. print (re.sub(r'(' + re.escape(n) + rf')|\b{k}\b',
  9. lambda m: m.group(1) if m.group(1) else c, s))
Success #stdin #stdout 0.02s 9332KB
stdin
Standard input is empty
stdout
F6[ab]
F3[1]