pattern match !

Jay Loden python at jayloden.com
Sat Jul 14 12:25:37 EDT 2007


hari.siri74 at gmail.com wrote:
>> A slightly more generic match in case your package names turn out to be less consistent than given in the test cases:
>>
>> #!/usr/bin/python
>>
>> import re
>> pattern = re.compile(r'(\w+?-(\d+[\.-])+\d+?)-\D+.*RPM')
>> pkgnames = ["hpsmh-1.1.1.2-0-RHEL3-Linux.RPM", "hpsmh-1.1.1.2-RHEL3-Linux.RPM"]
>> for pkg in pkgnames:
>>   matchObj = pattern.search(pkg)
>>   if matchObj:
>>     print matchObj.group(1)
>>
>> 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,
>>
>> -Jay
> 
> How about if i had something like 1-3 words in the application name:
> websphere-pk543-1.1.4.2-1-RHEL3-i386.rpm (in this case are 2 words)?

Try this instead then:

#!/usr/bin/python

import re
pattern = re.compile(r'((\w+?-)+?(\d+[\.-])+\d+?)-\D+.*RPM', re.I)
pkgnames = ["hpsmh-1.1.1.2-0-RHEL3-Linux.RPM", "hpsmh-1.1.1.2-RHEL3-Linux.RPM", "websphere-pk543-1.1.4.2-1-RHEL3-i386.rpm"]
for pkg in pkgnames:
  matchObj = pattern.search(pkg)
  if matchObj:
    print matchObj.group(1)



More information about the Python-list mailing list