Help me use re better

P Browning glpb at eis.bris.ac.uk
Tue Apr 24 02:38:10 EDT 2001


I've read AMK's RE HowTo but I think I'm missing something
obvious when it comes to substitutions. Any offers for
a more elegant solution to the program below gratefully
received. 

TIA Paul

------------------------
import string,re

directors = ['Prof A B Looney','Dr C D E Ftang','Ms H I J K Biscuit Barrel']
# I want no spaces between the initials
# Prof AB Looney
# Dr CDE Ftang
# Ms HIJK Biscuit Barrel

match_initials = re.compile(r'([A-Z] )+')

print
for director in directors:
    s = match_initials.search(director)
    inits = s.group(0)
    new_inits = string.replace(inits,' ','')
    new_director = string.replace(director,inits,new_inits + ' ')
    print director,new_director

-- 
--
 The Library, Tyndall Avenue, Univ. of Bristol, Bristol, BS8 1TJ, UK 
    E-mail: Paul.Browning at bristol.ac.uk  URL: http://www.bris.ac.uk/




More information about the Python-list mailing list