pattern match !

H

hari.siri74

Extract the application name with version from an RPM string like
hpsmh-1.1.1.2-0-RHEL3-Linux.RPM, i require to extract hpsmh-1.1.1.2-0
from above string. Sometimes the RPM string may be hpsmh-1.1.1.2-RHEL3-
Linux.RPM.
 
S

Steven D'Aprano

Extract the application name with version from an RPM string like
hpsmh-1.1.1.2-0-RHEL3-Linux.RPM, i require to extract hpsmh-1.1.1.2-0
from above string. Sometimes the RPM string may be hpsmh-1.1.1.2-RHEL3-
Linux.RPM.

Thank you for sharing.

The answer to your problem is here:
http://tinyurl.com/anel
 
A

Asun Friere

Extract the application name with version from an RPM string like
hpsmh-1.1.1.2-0-RHEL3-Linux.RPM, i require to extract hpsmh-1.1.1.2-0
from above string. Sometimes the RPM string may be hpsmh-1.1.1.2-RHEL3-
Linux.RPM.

Now that list-like splicing and indexing works on strings, why not
just splice the string, using .index to locate '-RHEL'?
 
H

Helmut Jarausch

Extract the application name with version from an RPM string like
hpsmh-1.1.1.2-0-RHEL3-Linux.RPM, i require to extract hpsmh-1.1.1.2-0
from above string. Sometimes the RPM string may be hpsmh-1.1.1.2-RHEL3-
Linux.RPM.

Have a try with

import re
P=re.compile(r'(\w+(?:[-.]\d+)+)-RHEL3-Linux\.RPM')
S="hpsmh-1.1.1.2-0-RHEL3-Linux.RPM"
PO= P.match(S)
if PO :
print PO.group(1)



--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
J

Jay Loden

Helmut said:
Extract the application name with version from an RPM string like
hpsmh-1.1.1.2-0-RHEL3-Linux.RPM, i require to extract hpsmh-1.1.1.2-0
from above string. Sometimes the RPM string may be hpsmh-1.1.1.2-RHEL3-
Linux.RPM.

Have a try with

import re
P=re.compile(r'(\w+(?:[-.]\d+)+)-RHEL3-Linux\.RPM')
S="hpsmh-1.1.1.2-0-RHEL3-Linux.RPM"
PO= P.match(S)
if PO :
print PO.group(1)


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
 
J

Jay Loden

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)
 
A

Asun Friere

import re
P=re.compile(r'(\w+(?:[-.]\d+)+)-RHEL3-Linux\.RPM')
S="hpsmh-1.1.1.2-0-RHEL3-Linux.RPM"
PO= P.match(S)
if PO :
print PO.group(1)

Isn't a regexp overkill here when this will do:

head = filename[:filename.index('-RHEL3')]

Of course if you need to make it more generic (as in Jay's solution
below), re is the way to go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top