Breaking up Strings correctly:

Michael Yanowitz m.yanowitz at kearfott.com
Mon Apr 9 08:19:00 EDT 2007


Hello:

   I have been searching for an easy solution, and hopefully one
has already been written, so I don't want to reinvent the wheel:

   Suppose I have a string of expressions such as:
"((($IP = "127.1.2.3") AND ($AX < 15)) OR (($IP = "127.1.2.4") AND ($AY !=
0)))
  I would like to split up into something like:
[ "OR",
  "(($IP = "127.1.2.3") AND ($AX < 15))",
  "(($IP = "127.1.2.4") AND ($AY != 0))" ]

     which I may then decide to or not to further split into:
[ "OR",
  ["AND", "($IP = "127.1.2.3")", "($AX < 15)"],
  ["AND", "(($IP = "127.1.2.4")", ($AY != 0))"] ]

  Is there an easy way to do this?
I tried using regular expressions, re, but I don't think it is
recursive enough. I really want to break it up from:
(E1 AND_or_OR E2) and make that int [AND_or_OR, E1, E2]
  and apply the same to E1 and E2 recursively until E1[0] != '('

   But the main problem I am running to is, how do I split this up
by outer parentheseis. So that I get the proper '(' and ')' to split
this upper correctly?


Thanks in advance:
Michael Yanowitz





More information about the Python-list mailing list