Help! Complex Pattern Extraction with Key/Value Pairs and Reg Exp?

Discussion in 'Perl Misc' started by aekalman, Nov 18, 2004.

  1. aekalman

    aekalman Guest

    I'm really having difficulty with regular expressions -- perhaps
    someone can help?

    BACKGROUND: The strings:

    sfiaravrt-7s-ec-sf-64.r90
    sliaravrt-7s-ec-sf-64-i.r90
    sfiaravra-6s-ec-sf.r90
    sliaravrd-5l-sf-64.r90
    sliaravrt-1s.r90
    ....

    are various file names in a group of files.

    Within a complex makefile system for generating libraries, I use the
    following shell commands and reg exp to extract information from the
    file names and pass that onto the compiler (via recursive make) as
    command-line arguments, using:

    *****

    INDEX=$(shell expr '$(1)' : '$(2)')

    PAIR=$(filter $(1)@%,$(2))

    VAL=$(patsubst $(1)@%,%,$(2))

    GETVAL = $(call VAL,$(call INDEX,$(1),$(2)),$(call PAIR,$(call
    INDEX,$(1),$(2)),$(3)))

    *****

    TYPE_VALS = sf@OSF sl@OSL
    FAMILY_VALS = iaravr@iaravr
    CONFIG_VALS = a@OSA d@OSD e@OSE m@OSM t@OST
    CPU_VALS = 0@0 1@1 2@2 3@3 4@4 5@5 6@6 7@7
    MODEL_VALS = l@OSL s@OSS t@OST
    CORE_VALS = -ec@OSE
    FLASH_VALS = -sf@OSS
    DOUBLES_VALS = -64@OSD
    OPTION_VALS = -i@OSI

    $(LIBRARIES):
    $(MAKE) -f Makefile2 $@ \
    T=$(call GETVAL,$@,\(..\), $(TYPE_VALS)) \
    F=$(call GETVAL,$@,..\(......\).*\.r90, $(FAMILY_VALS)) \
    C=$(call GETVAL,$@,........\(.\).*\.r90, $(CONFIG_VALS)) \
    CPU=$(call GETVAL,$@,..........\(.\).*\.r90,$(CPU_VALS)) \
    MODEL=$(call GETVAL,$@,...........\(.\).*\.r90,$(MODEL_VALS)) \
    CORE=$(call GETVAL,$@,............\(.*\).r90,$(CORE_VALS)) \
    FLASH=$(call GETVAL,$@,............\(.*\).r90, $(FLASH_VALS)) \
    DOUBLES=$(call GETVAL,$@,............\(.*\).r90,$(DOUBLES_VALS)) \
    O=$(call GETVAL,$@,............\(.*\).r90, $(OPTION_VALS))

    *****

    When you run make, you get something out like this:

    make -f Makefile2 sfiaravrt-7s-ec-sf-64.r90 T=OSF F=iaravr C=OST CPU=7
    ....

    The reg exp patterns are in the calls to GETVAL. This systems works
    GREAT for filenames of constant length and format, and was suggest by
    Jonathan Kamens to me back in '01.



    PROBLEM: Using regular expressions, I am able to find patterns at the
    start of the strings (like the (f,l) pair in the second position, or
    the (a,d,t) trio in the ninth position) just fine because their
    positions are fixed -- easy. The GETVAL macro returns a string (e.g.
    OSF) that is passed onto the compiler via MAKE as T=OSF. Note that a
    match must occur, and so the variable (in this case, T) is always
    defined.

    It's starting with the 13th position that I'm having trouble. I can't
    seem to build a reg exp that excludes the first 12 characters, and
    only searches from the 13th character onwards, up to the .r90 at the
    end. I want to search only the 13th through last positions of the
    filenames for the patterns:

    -ec
    -sf
    -64
    -i

    and, if found, set the associated variable (e.g. CORE) to the value in
    its key/value pairs list (e.g. CORE=OSE for "-ec is present"), o/wise
    leave it undefined or defined to OSNONE (e.g. CORE=OSNONE for "-ec was
    not found in the filename").

    How do I do that?

    Can anyone help? Please?

    Thanks,

    --Andrew Kalman
    aek at pumpkin inc dot com
    aekalman, Nov 18, 2004
    #1
    1. Advertising

  2. aekalman

    John Bokma Guest

    John Bokma, Nov 18, 2004
    #2
    1. Advertising

  3. Re: Help! Complex Pattern Extraction with Key/Value Pairs and RegExp?

    John Bokma wrote:
    > aekalman wrote:
    >
    >
    >>I'm really having difficulty with regular expressions -- perhaps
    >>someone can help?

    >
    >
    > And your *Perl* question is?
    >
    >


    Where's the best place to ask regexp questions, if not here?

    --

    ------------------------------------------------------
    Andrew Kalman
    aek ... at ... pumpkininc ... dot ... com
    Andrew E. Kalman, Nov 18, 2004
    #3
  4. aekalman

    Uri Guttman Guest

    Re: Help! Complex Pattern Extraction with Key/Value Pairs and RegExp?

    >>>>> "AEK" == Andrew E Kalman <> writes:

    AEK> John Bokma wrote:
    >> aekalman wrote:
    >>
    >>> I'm really having difficulty with regular expressions -- perhaps
    >>> someone can help?

    >> And your *Perl* question is?
    >>


    AEK> Where's the best place to ask regexp questions, if not here?

    some group that discusses regexes. this is cl.perl.misc. regexes are in
    many langs and tools.

    all that lang foo is better than perl crap is just regex envy with
    everyone claiming pcre and lying as nothing is really compatible with
    perl regexes but perl.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 18, 2004
    #4
  5. "Andrew E. Kalman" <> writes:
    > John Bokma wrote:
    > > aekalman wrote:
    > >
    > >>I'm really having difficulty with regular expressions -- perhaps
    > >>someone can help?

    > > And your *Perl* question is?
    > >

    >
    > Where's the best place to ask regexp questions, if not here?


    If your programming environment is Unix, try comp.unix.programmer.

    There is no group dedicated to regular expressions. There are many
    versions of regular expressions, and the ones in Perl are among the
    most powerful. A Perl answer may not work at all in 'sh', 'sed',
    'grep' or 'make'. I think that asking a regular expression question
    here in the hope of finding that Perl is the right tool wouldn't be
    wrong, if you make it clear that that is the purpose of your
    question.
    Arndt Jonasson, Nov 18, 2004
    #5
  6. aekalman

    aekalman Guest

    Arndt Jonasson <> wrote in message news:<>...
    > There is no group dedicated to regular expressions. There are many
    > versions of regular expressions, and the ones in Perl are among the
    > most powerful. A Perl answer may not work at all in 'sh', 'sed',
    > 'grep' or 'make'. I think that asking a regular expression question
    > here in the hope of finding that Perl is the right tool wouldn't be
    > wrong, if you make it clear that that is the purpose of your
    > question.


    In this particular case I'm running in Cygwin's bash shell on Win32
    machines.

    While I've got other utilities implemented as Perl scripts, this one
    is an odd beast, as it's primarily withing GNU make but uses the
    shell's expr to do some regex processing outside of make.

    I posted to this group mainly because of my perceived tight linkage
    between Perl and regexp, thinking that my problem was mainly one of
    not understanding regexp well enough, and someone with more epxerience
    could enlighten me. Since my original posting, I've discovered that
    the shell's expr automatically prepends the '^' character to my
    pattern (aaaaaah!), and that's the root cause of my problem. So it's
    not a regexp problem, but rather, a shell problem. No obvious
    workaround (to me, at least).

    Oh, why can't tools be easier to use? :(

    Thanks for all the replies ...

    --Andrew Kalman
    aek at pumpkininc dot com
    aekalman, Nov 19, 2004
    #6
  7. aekalman

    Ben Morrow Guest

    OT expr(1) regexen

    Quoth (aekalman):
    > could enlighten me. Since my original posting, I've discovered that
    > the shell's expr automatically prepends the '^' character to my
    > pattern (aaaaaah!), and that's the root cause of my problem. So it's
    > not a regexp problem, but rather, a shell problem. No obvious
    > workaround (to me, at least).


    Prepend .* to your pattern.

    Ben

    --
    We do not stop playing because we grow old;
    we grow old because we stop playing.
    Ben Morrow, Nov 22, 2004
    #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. PerlE

    Reg Exp Help

    PerlE, Jan 30, 2004, in forum: Perl
    Replies:
    0
    Views:
    496
    PerlE
    Jan 30, 2004
  2. Aristotle

    Help needed with reg exp please

    Aristotle, Sep 4, 2004, in forum: Perl
    Replies:
    4
    Views:
    475
    Gunnar Hjalmarsson
    Sep 4, 2004
  3. Markus Dehmann

    key-value pairs: key consists of 3 ints

    Markus Dehmann, Jan 15, 2006, in forum: C++
    Replies:
    13
    Views:
    625
    Richard Herring
    Jan 23, 2006
  4. Antonio Quinonez
    Replies:
    2
    Views:
    154
    Antonio Quinonez
    Aug 14, 2003
  5. Ivo
    Replies:
    5
    Views:
    87
    mick white
    Jan 11, 2006
Loading...

Share This Page