Replace the content in between using python regex -


i have string:

'[text], text, {text}, itextm' 

i should end with:

'[replaced], text, {replaced}, ireplacedm' 

how can python re module? thanks!

in [1]: s='[text], text, {text}, itextm' in [2]: import re in [3]: re.sub(r'([[{])[^\]}]*',r'\1replaced',s) out[3]: '[replaced], text, {replaced}, itextm' 

edit

ah... didn't notice last part of example, ireplacedm!

now should work example:

in [8]: re.sub(r'([^ ,])text([^ ,])',r'\1replaced\2',s)                                                                                                                      out[8]: '[replaced], text, {replaced}, ireplacedm' 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -