text processing problem

Leif K-Brooks eurleif at ecritters.biz
Fri Apr 8 17:16:40 EDT 2005


Maurice LING wrote:
> I'm looking for a way to do this: I need to scan a text (paragraph or 
> so) and look for occurrences of "<text-x> (<text-x>)". That is, if the 
> text just before the open bracket is the same as the text in the 
> brackets, then I have to delete the brackets, with the text in it.


How's this?

import re

bracket_re = re.compile(r'(.*?)\s*\(\1\)')

def remove_brackets(text):
     return bracket_re.sub('\\1', text)



More information about the Python-list mailing list