String pattern matching

Kent Johnson kent at kentsjohnson.com
Mon Apr 3 13:33:34 EDT 2006


Eddie Corns wrote:
> 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
>

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

Cool! I have to get in on the fun :-)

This program uses Sam Wilmott's library to find one solution. It is a 
simple translation of your program above. I couldn't figure out how to 
get multiple solutions.
http://www.wilmott.ca/python/patternmatching.html

import string
from patterns_b import *

subject = MatchingInput ('/abcaaab/abca/eeabcac/')
Letters = AnyOfP(string.letters)[1:]

subject ^ IsP('/') & Letters >> 'x' & Letters >> 'y' & Letters >> 'z' & 
IsP('/') \
             & AnotherP('x') & AnotherP('y') & IsP('/') \
             & Letters >> 'v' & AnotherP('x') & Letters >> 'w' & IsP('/')

print 'v =', subject['v']
print 'w =', subject['w']
print 'x =', subject['x']
print 'y =', subject['y']
print 'z =', subject['z']


Output is
v = ee
w = ac
x = abc
y = a
z = aab


Kent



More information about the Python-list mailing list