In many programming languages, the following
find foo([a-z]+)bar and replace with GOO\\U\\1GAR
foo([a-z]+)bar
GOO\\U\\1GAR
will result in the entire match bei
You could use some variation of this:
s = 'foohellobar' def replfunc(m): return m.groups()[0]+m.groups()[1].upper()+m.groups()[2] re.sub('(foo)([a-z]+)(bar)',replfunc,s)
gives the output:
'fooHELLObar'