Recursive regular expression (or alternative)
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Recursive regular expression (or alternative)

From: André Hänsel <andre@webkr.de>
Date: Wed Apr 12 2006 - 22:04:46 CEST

Hi,

I'm looking for a pattern to parse the following subject:

blabla(cond1)?{abc}:{def}blabla(cond2)?{qwe(cond3)?{poi}:{iop}}

Probably I can't do that by executing a regexp _once_, so I need some
kind of recursive call to get those two lists (e.g. in an array):
array(
 0 => array("cond1","abc","def"),
 1 => array("cond2","qwe(cond3)?{poi}:{iop}","")
)
and for the second one then I need the array:
array(
 0 => array("cond3","poi","iop")
)

Or in words: In a configuration file I have _nested_ conditional blocks
in the format (condition)?{content_when_true}:{content_when_false}
while the false content ist optional.

I built the following pattern, but thats just a bad guess:
/\(([a-zA-Z0-9_ &|]+)\)\?\{(.*)\}(\:\{(.*)\})?/
It matches both blocks when it's greedy and ignores the nested block
when it's ungreedy.

If you have any completly different solution I'll be glad to hear it,
too. :)

Best regards,
André
Received on Mon May 1 02:54:32 2006