I have a string
statement = 'P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R)'
I want to store each string that is within parentheses, like this:
['Q ∨ R', 'P ∧ Q', 'P ∧ R']
How can this be done?
I have a string
statement = 'P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R)'
I want to store each string that is within parentheses, like this:
['Q ∨ R', 'P ∧ Q', 'P ∧ R']
How can this be done?
>>> import re
>>> [s[1:-1] for s in re.findall(r'\(.*?\)', 'P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R)')]
['Q ∨ R', 'P ∨ Q', 'P ∧ R']
It's a good use-case for regex:
>>> import re
>>> re.findall(r'\((.*?)\)', statement)
['Q ∨ R', 'P ∧ Q', 'P ∧ R']
The ?
character in the pattern is a non-greedy modifier suffix.