matching and extracting...
Mike C. Fletcher
mcfletch at rogers.com
Tue Jan 14 18:33:24 EST 2003
I won't try to write the whole thing here, but basically, you're doing
pattern matching; use one of the pattern matching engines (there's lots
of them). Transform your pattern definition into an appropriate
definition for that library.
For instance with re:
-p gives you the pattern as a simple string
a0-p -> a0
-x gives you a named group named as the pattern with content == to ".."
i1-x -> (?P<i1>..)
-? gives you an optional group with the pattern
i8-? -> (i8)?
So, just do something like:
" ".join( [ transform( s ) for s in pattern.split() ] )
to create your pattern (with suitably defined transform function as
described above), and then run re.match against your test-string and
your pattern.
Any of the other pattern-matching engines might work just as well, but
re comes standard with Python.
HTH,
Mike
Shagshag wrote:
>Here is my problem : say i have items ix, some are "to discover" (must
>be extracted "-x"), some are "needed" (must be present "-p") and some
>are indifferent ("-?"). for example, i have sequence like :
>
>s = "a0 a1 a2 a3 a4 a5 a6 a7 a8"
>
>i would like to check if my sequence is matching sequence like :
>
>m = "a0-p i1-x i2-x i3-? a4-p i5-x i6-? i7-x i8-?"
>
>and get result like :
>
>m is matching s, i1 is a1, i2 is a3, i5 is a5, i7 is a7 (in python a
>"true" and a dict)
>
>could python help me ? how should i do this ? must i rely on regexps ?
>(entirely or prefer text matching ?) have you any ideas, comments of a
>pythonic way to implement this ?
>
>thanks in advance,
>
>s13.
>
>
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list