-1

I have a string that looks something like this -

((COL1==VAL1)+(COL2==VAL2))

I want to convert this to following format:

((a.col1 == 'VAL1') & (a.col2 == 'VAL2'))

Another example -

From:

((COL1==VAL1)|(COL2==VAL2/A))

TO: ((a.col1 == 'VAL1') | (a.col2 == 'VAL2/A'))

can someone help please?

Code:

word = Word(alphas + alphanums + "+-_<> ")

open = Literal('(')

close = Literal(')')

single_quote = Literal('\'')

and_ = Literal('+')

equal = Literal('=')

comma = Literal(',')

or_ = Literal('|')

right_hand_value = word 

left_hand_value = word

func = OneOrMore(open) + left_hand_value + OneOrMore(equal) + right_hand_value + OneOrMore(close)

results = func.parseString(string)

This kind of works only for one expression: (a.col1 == 'VAL1'). but I want this to handle multiple expressions.

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
santosh
  • 57
  • 7

1 Answers1

1

This is actually a good candidate for pyparsing's infixNotation method. Define an expression for your equality comparison, then use it as the operand in a simple NOT/AND/OR infixNotation parser.

infixNotation will take care of all the nesting of parentheses, and will also help you apply precedence of operations.

PaulMcG
  • 62,419
  • 16
  • 94
  • 130