[Tutor] regular expressions
Kent Johnson
kent37 at tds.net
Tue Aug 5 14:41:41 CEST 2008
On Tue, Aug 5, 2008 at 7:01 AM, Jim Morcombe <jmorcombe at westnet.com.au> wrote:
> Could someone please give me some help using the "re" module.
> 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.
Please show the specific error message you get; "it complained" is too vague.
>
> 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
Again, this is too vague. Show us exactly what you tried and what
result you got.
BTW you don't need the re module to replace fixed strings with new
strings, you can use the replace() method of the string:
In [1]: text = "Bill Smith is nice"
In [5]: text.replace('Smith', '')
Out[5]: 'Bill is nice'
Kent
More information about the Tutor
mailing list