I want to see if a pair of parenthesis is next to another pair of parenthesis in a string, But: I want it not to be detected inside a pair of parenthesis. (like this)
>>> s = "(text(some other stuff))"
>>> check_parenthesis(s)
False
>>> s = "(text)(more text)"
>>> check_parenthesis(s)
True
What i have tried:
>>> s = "(text)(more text)"
>>> s.find(")(") != -1 # Thing is: if i put ')(' inside a pair of parenthesis, it will return 'True' when i expect 'False' (I expect when ')(' in pair of parenthesis, it return 'False', but if it's like this: '(text)(more text)' then i expect 'True')
True
>>> s = "(text)()"
>>> s.find(")(") != -1 # Expected 'False' because ')(' is inside pair of parenthesis
True
I have experimented with code for almost 30 minutes
Even searched online
How do i do this? (Debian/Ubuntu, Python-3.8.10)
CodePudding user response:
You're question is still unclear, however this passes your defined test. (tho i don't see any need for it)
def check_parenthesis(text):
position = text.find(')(') # return the starting position of the sub-string
if (position != -1) and (position < len(text) - 2):
if text[position 2] != ")":
return True
return False
check_parenthesis('(test)()')=False
check_parenthesis('(test)(some_more)')=True
check_parenthesis('(test(some_more))')=False