[Tutor] regular expressions

Jim Morcombe jmorcombe at westnet.com.au
Tue Aug 5 13:01:59 CEST 2008

Could someone please give me some help using the "re" module.

This works:
import re

text = "Jim is a good guy"

s2 = re.sub('Jim', 'Fred', text)
print s2

and I get "Fred is a good guy"
If I have:
text = "Bill Smith is nice"
how do I get rid of "Smith" and just have
"Bill is nice"

I tried
s2 = re.sub('Smith', '', text)
but it complained.

If I have:
text = "Jim likes a girl (Susan)"
and I want to get rid of "(Susan)", how do I do this.

First, the "(" seems to muck things up.
Second, how do I just use "re" to delete characters.  I tried using 
"sub", but it doesn't seem to like

Jim Morcombe

More information about the Tutor mailing list