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

A

aekalman

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
 
U

Uri Guttman

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
 
A

Arndt Jonasson

Andrew E. Kalman said:
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.
 
A

aekalman

Arndt Jonasson said:
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
 
B

Ben Morrow

Quoth (e-mail address removed) (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
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top