Here is a set of strings with balanced parentheses.
yes1 = "x (a b c) y"
yes2 = "((((x))))"
yes3 = "a (b (c d) ((e)) f) (g)"
Here is a set of strings with unbalanced parentheses.
no1 = "(a b c"
no2 = "((())))"
no3 = "a (b (c d) (e)) f) (g)"
Write a function check_balanced
that uses regular expressions
to check whether the parentheses are balanced.
Note that you can't do this with a single regular expression,
you need to write a little loop around it.
Your code structure might differ a little from the
function below, but it shouldn't be much longer.
import re
def check_balanced(s):
...
while ...:
...
... = re.subn(...)
...
...
Now show that it works.
print check_balanced(yes1)
print check_balanced(yes2)
print check_balanced(yes3)
print check_balanced(no1)
print check_balanced(no2)
print check_balanced(no3)