String pattern matching

Eddie Corns eddie at holyrood.ed.ac.uk
Mon Apr 3 11:12:20 EDT 2006


"Jim Lewis" <jimlewis at miclog.com> writes:

>Anyone have experience with string pattern matching?
>I need a fast way to match variables to strings. Example:

>string - variables
>============
>abcaaab - xyz
>abca - xy
>eeabcac - vxw

>x matches abc
>y matches a
>z matches aab
>w maches ac
>v maches ee

Off topic I know but I've been learning snobol pattern matching recently so I
thought I'd give this one a bash.  Here is my effort:

        define('foo(str)')
        &fullscan = 1
        '/abcaaab/abca/eeabcac/' '/' arb $ x arb $ y arb $ z '/' *x *y '/'
+		 arb $ v *x arb $ w '/' *foo(v '/' w '/' x '/' y '/' z) :F(end)
foo     output = str     :(FRETURN)

end

Good eh?  Here's the output:

/eeabcac//abca/aab
e/eabcac//abca/aab
ee/abcac//abca/aab
eea/bcac//abca/aab
eeab/cac//abca/aab
eeabc/ac//abca/aab
eeabca/c//abca/aab
eeabcac///abca/aab
ee/bcac/a/bca/aab
eeabc/c/a/bca/aab
ee/cac/ab/ca/aab
ee/ac/abc/a/aab
ee/c/abca//aab

Removing empty entries is quite easy:

        '/abcaaab/abca/eeabcac/' '/' arb $ x arb $ y arb $ z '/' *x *y '/'
+		 arb $ v *x arb $ w '/' *differ(v) *differ(w) *differ(x)
+		 *differ(y) *differ(z) *foo(v '/' w '/' x '/' y '/' z) :F(end)

generates:

ee/bcac/a/bca/aab
eeabc/c/a/bca/aab
ee/cac/ab/ca/aab
ee/ac/abc/a/aab

If I get time I'll try to get this working in Sam Wilmott's Python pattern
matching library.

What fun!

Eddie



More information about the Python-list mailing list