parsing a file name

Roberto Bonvallet Roberto.Bonvallet at cern.ch
Fri Jan 12 15:25:07 EST 2007


CSUIDL PROGRAMMEr wrote:
> I have a filename
> cairo-2.3.4.src.rpm
> Is there any way i can only get 2.3.4 from this file name

 >>> a = "cairo-2.3.4.src.rpm"
 >>> import re
 >>> re.compile(r"\d+([.]\d+)*").search(a).group(0)
 '2.3.4'
 >>> a.split("-")[-1][:-len(".src.rpm")]
 '2.3.4'
 >>> ".".join(map(str, range(2, 5)))
 '2.3.4'

-- 
Roberto Bonvallet



More information about the Python-list mailing list