Click to See Complete Forum and Search --> : Elegant pattern matching


tbirnseth
08-02-2007, 12:30 AM
I'm looking for an elegant way to solve this problem....

I'd like to parse a string such as "%[foo.bar(%[bar.foo(%[zap.boing]%)]%)]% and more %[last.chunk]% the end" with the following rules:

1) '%[' and ']%' delimit variable names (optionally nested).
2) '(' and ')' (optional) delimit arguments to variable names, might contain another variable name and arguments.
3) Needs to recurse if either of the above conditions are nested.

Goal is that the argument lists would be subsequently parsed based on which variable they are contained in. So result would be something like:

array('zap.boring'=> '',
'bar.foo' => 'zap.boring',
'foo.bar' => 'bar.foo',
0 => ' and more ',
'last.chunk' => '',
1 => ' the end'
);


Order is important so it can all be put back together with variable replacement. Variable names are associative elements, literals are numeric.


That I can recursively parse! But really struggling to get the right tokenizing. Anyone want to give it a shot?

tony