Searching and replacing text ?

Jeff jeffober at gmail.com
Fri May 2 15:46:06 EDT 2008


If you know that, for instance, every occurrence of '[[' needs to be
replaced with ']]', then it is much faster to use regular string
methods.  If you have the text stored in the variable foo:

foo = foo.replace('[[', '[').replace(']]', ']').replace('->', '')

If you need to recognize the actual pattern, use REs:

regex = re.compile(r'\[\[(.+?) -> (.+?)\]\]')
regex.sub(r'[\1 \2]', foo)



More information about the Python-list mailing list