pattern match !

Jay Loden python at
Wed Jul 11 23:12:51 CEST 2007

Helmut Jarausch wrote:
> hari.siri74 at wrote:
>> Extract the application name with version from an RPM string like
>> hpsmh-, i require to extract hpsmh-
>> from above string. Sometimes the RPM string may be hpsmh-
>> Linux.RPM.
> Have a try with
> import re
> P=re.compile(r'(\w+(?:[-.]\d+)+)-RHEL3-Linux\.RPM')
> S="hpsmh-"
> PO= P.match(S)
> if  PO :
>    print

A slightly more generic match in case your package names turn out to be less consistent than given in the test cases:


import re
pattern = re.compile(r'(\w+?-(\d+[\.-])+\d+?)-\D+.*RPM')
pkgnames = ["hpsmh-", "hpsmh-"]
for pkg in pkgnames:
  matchObj =
  if matchObj:

Still assumes it will end in RPM (all caps), but if you add the flag "re.I" to the re.compile() call, it will match case-insensitive. 

Hope that helps, 


More information about the Python-list mailing list