>>> p = re.compile('section{ ( [^}]* ) }', re.VERBOSE) >>> p.sub(r'subsection{\1}','section{First} section{second}') 'subsection{First} subsection{second}' ====== references ====== *[[http://www.amk.ca/python/howto/regex/|Regular Expression HOWTO]]