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
Post a Comment