Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > pattern match !

Reply
Thread Tools

pattern match !

 
 
hari.siri74@gmail.com
Guest
Posts: n/a
 
      07-11-2007
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.

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-11-2007
On Wed, 11 Jul 2007 03:40:06 +0000, hari.siri74 wrote:

> 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


--
Steven.
 
Reply With Quote
 
 
 
 
Asun Friere
Guest
Posts: n/a
 
      07-11-2007
On Jul 11, 1:40 pm, hari.sir...@gmail.com wrote:
> 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'?

 
Reply With Quote
 
Helmut Jarausch
Guest
Posts: n/a
 
      07-11-2007
wrote:
> 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
 
Reply With Quote
 
Jay Loden
Guest
Posts: n/a
 
      07-11-2007

Helmut Jarausch wrote:
> wrote:
>> 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
 
Reply With Quote
 
Jay Loden
Guest
Posts: n/a
 
      07-14-2007
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)
 
Reply With Quote
 
Asun Friere
Guest
Posts: n/a
 
      07-16-2007
On Jul 11, 9:29 pm, Helmut Jarausch <jarau...@igpm.rwth-aachen.de>
wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
Match doesn't match Volkan Civelek Ruby 4 07-19-2006 07:44 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57