-2

I have a string like '((30 >=50) and (20 <= 100)) or (52 > 35)'.

I want to convert this to a regular expression and evaluate.

For example, value = ((30 >=50) and (20 <= 100)) or (52 > 35)

Should return true or false based on evaluating this expression

Christopher
  • 895
  • 6
  • 15

2 Answers2

1

You cannot determine if a string with arbitrary number of parentheses is actually syntactically correct (i.e. every parenthesis is closed) using regular expressions because it's fundamentally more complex (you need a stack-based solution to solve a context-free grammar class problem) than what a regular expression can handle (a simple DFA).

So you need to reconsider how you're approaching this problem.

That said, here's a small program that checks if the expression has balanced number of parentheses and then evaluates the result.

def is_correct_expression(expr: str) -> bool:
    pars = []
    try:
        for c in expr:
            if c == '(':
                pars.append(c)
            if c == ')':
                pars.pop()
        return True
    except IndexError:
        return False


if __name__ == '__main__':
    expr = '((30 >=50) and (20 <= 100)) or (52 > 35)'

    if is_correct_expression(expr):
        result = eval(expr)
        print(result)
    else:
        print('invalid expression')

output:

True
abdusco
  • 9,700
  • 2
  • 27
  • 44
  • thank you. using eval function solves my problem. and yes using the is_correct_expression function, i could validate my expression before evaluating it. – Arulanand Nagarajan Jul 17 '19 at 08:17
  • Also one more question. How can i replace a list of strings with another. For example i have 2 lists. list1 = ['aa', 'bb', 'cc'] and list2 = ['80.0', '20.0', '25.0']. I want to replace the list1 strings with respective list2 strings in the expression OP = ((aa >= 50) and (bb<=100)) or (cc == 25). Is there a single python command to replace a list of strings with another? – Arulanand Nagarajan Jul 19 '19 at 10:24
1

You can use eval() to get the result of your string.

In your case, it will be value = eval('((30 >=50) and (20 <= 100)) or (52 > 35)')

jtan354
  • 84
  • 9
  • I believe that what OP is talking about is not regex, but the literal meaning of a regular expression. OP likely does not know what regex is. – jtan354 Jul 17 '19 at 06:51
  • @ArulanandNagarajan Please accept my answer as the correct answer if it helped you :) – jtan354 Jul 17 '19 at 08:26
  • Also one more question. How can i replace a list of strings with another. For example i have 2 lists. list1 = ['aa', 'bb', 'cc'] and list2 = ['80.0', '20.0', '25.0']. I want to replace the list1 strings with respective list2 strings in the expression OP = ((aa >= 50) and (bb<=100)) or (cc == 25). Is there a single python command to replace a list of strings with another? – Arulanand Nagarajan Jul 19 '19 at 10:24
  • @ArulanandNagarajan [This link](https://stackoverflow.com/questions/2582138/finding-and-replacing-elements-in-a-list-python) might be able to help you. – jtan354 Jul 22 '19 at 01:23