pattern match !

Discussion in 'Python' started by hari.siri74@gmail.com, Jul 11, 2007.

  1. Guest

    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.
    , Jul 11, 2007
    #1
    1. Advertising

  2. 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.
    Steven D'Aprano, Jul 11, 2007
    #2
    1. Advertising

  3. Asun Friere Guest

    On Jul 11, 1:40 pm, 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'?
    Asun Friere, Jul 11, 2007
    #3
  4. 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
    Helmut Jarausch, Jul 11, 2007
    #4
  5. Jay Loden Guest

    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
    Jay Loden, Jul 11, 2007
    #5
  6. Jay Loden Guest

    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)
    Jay Loden, Jul 14, 2007
    #6
  7. Asun Friere Guest

    On Jul 11, 9:29 pm, Helmut Jarausch <-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.
    Asun Friere, Jul 16, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    636
  2. Victor
    Replies:
    2
    Views:
    643
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    369
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    480
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    163
Loading...

Share This Page