Regex search and replace substring in Python -
i need regular expression in python.
i have string this:
>>> s = '[i1]scale=-2:givenheight_1[o1];'
how can remove givenheight_1
, turn string this?
>>> '[i1]scale=-2:360[o1];'
is there efficient one-liner regex such job?
update 1: regex far not working:
re.sub('givenheight_1[o1]', '360[o1]', s)
you can use positive look around
re.sub :
>>> s = '[i1]scale=-2:givenheight_1[o1];' >>> re.sub(r'(?<=:).*(?=\[)','360',s) '[i1]scale=-2:360[o1];'
the preceding regex replace thing came after :
, before [
'360'
.
or based on need can use str.replace
directly :
>>> s.replace('givenheight_1','360') '[i1]scale=-2:360[o1];'
Comments
Post a Comment