RegExp, Python and strings

Andrew M. Kuchling akuchlin at mems-exchange.org
Sat Jan 8 14:44:05 EST 2000


"Matthias Huening" <matthias.huening at univie.ac.at> writes:
> In PERL I can do things like this (in one line):
> $A = "Rossum, Guido van; Harms, Daryl; Python, Franz-Josef";
> $A =~ s/([A-Z])[\w]+(?![ \w\-]+,)([ ;-]|$)/$1.$2/g;
> This RegExp results in: "Rossum, G. van; Harms, D.; Python, F.-J."

re.sub will do it; consult the documentation for the 're' module.

>>> import re
>>> A = "Rossum, Guido van; Harms, Daryl; Python, Franz-Josef";
>>> re.sub(r'([A-Z])[\w]+(?![ \w\-]+,)([ ;-]|$)', r'\1.\2', A)
'Rossum, G. van; Harms, D.; Python, F.-J.'

-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
    "Hmph. What the hell would you know? You're a dog."
    "Did I ever say I wasn't?"
    -- Destruction and Barnabas, in SANDMAN #43: "Brief Lives:3"




More information about the Python-list mailing list