Regex help...pretty please?

Tim Chase python.list at tim.thechases.com
Wed Aug 23 15:22:42 EDT 2006


> cond(a,b,c)
> 
> and I want them to look like this:
> 
> cond(c,a,b)
> 
> but it gets a little more complicated because the conds themselves may
> have conds within, like the following:
> 
> cond(0,cond(c,cond(e,cond(g,h,(a<f)),(a<d)),(a<b)),(a<1))

Regexps are *really* *REALLY* *bad* at arbitrarily nested 
structures.  really.

Sounds more like you want something like a lex/yacc sort of 
solution.  IIUC, pyparsing may do the trick for you.  I'm not a 
pyparsing wonk, but I can hold my own when it comes to crazy 
regexps, and can tell you from experience that regexps are *not* 
a good path to try and go down for this problem.

Many times, a regexp can be hammered into solving problems 
superior solutions than employing regexps.  This case is not even 
one of those.

If you know the maximum depth of nesting you'll encounter, you 
can do some hackish stunts to shoehorn regexps to solve the 
problem.  But if they are truely of arbitrary nesting-depth, 
*good* *luck*! :)

-tkc







More information about the Python-list mailing list