Python Regex for a closing parenthesis without an opening parenthesis
I am looking to write a regex pattern than recognizes when my text has a single open parenthesis. I wrote this code below to search for a single left hand parenthesis with no right hand parenthesis. I tested the pattern on regex101.com, and it seems to recognize the pattern fine. But when I run this code below, it returns None.
str1=' 8347(23402234'
test_this1=re.compile(r'''^\s*\([^\)]*$''',re.VERBOSE)
print(test_this1.match(str1))
I tested a similar code to identify if a string has only a closing parenthesis with no opening parenthesis.
str2=' 834723402)234'
test_this2=re.compile(r'''^\s*[^\(]*\)$''',re.VERBOSE)
print(test_this2.match(str2))
Similarly this resulted in None even though regex101.com showed that it should recognize the string.
Is there a way to also combine the two and write a single regex pattern where it looks for any strings with an unpaired parenthesis?
Read more here: Source link
