Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

From: Florian Diesch <diesch@spamfence.net>
Date: Wed Jul 13 2005 - 16:21:32 CEST

Ric Da Force <ric@next-level.com.au> wrote:
> I have a string such as 'C1, C2, C3'. Without assuming that each bit of
> text is of fixed size, what is the easiest way to change this list so that
> it reads:
> 'C1, C2 and C3' regardless of the length of the string.

>>> import re
>>> data = "the first bit, then the second, finally the third"
>>> re.sub(r"(.*),", r"\1 and", data)
'the first bit, then the second and finally the third'
>>>

   Florian

-- 
begin  signature_virus
 Hi! I'm a signature virus. Please copy me to your signature to help me spread.
end
[plaintext signature_virus.1121264492.317287226]
Received on Thu Sep 29 16:55:52 2005