regex – Python – Iterate once over string to find all substrings and their positions

I have the following python code using regex that finds the substrings “¬[“, “[“, “¬(“, “(“, “)”, “]” and get their positions
(I transformed the “¬[” and “¬(” into “[” and “(“)

import re

expression = "¬[P∧¬(¬T∧R)]∧(T→¬P)"
# [[0 "¬["], [4 "¬("], [13 "("], [10 ")"], [18 ")"], [11 "]"]]

lsqb = [[match.start(), "["] for match in re.finditer("\¬\[|\[", expression)]
lpar = [[match.start(), "("] for match in re.finditer("\¬\(|\(", expression)]
rpar = [[match.start(), ")"] for  match in re.finditer("\)", expression)]
rsqb = [[match.start(), "]"] for match in re.finditer("\]", expression)]
all = lsqb + lpar + rpar + rsqb

print(lsqb) # [[0, '[']]
print(lpar) # [[4, '('], [13, '(']]
print(rpar) # [[10, ')'], [18, ')']]
print(rsqb) # [[11, ']']]

print(all) # [[0, '['], [4, '('], [13, '('], [10, ')'], [18, ')'], [11, ']']]

The issue is that I’m iterating over the string 4 times (once for each type of parentheses I want to find the position of… )
I’d like to get rid of all those parentheses variables and just have the “all” one while iterating only once over the string and still getting:
[[0, ‘[‘], [4, ‘(‘], [13, ‘(‘], [10, ‘)’], [18, ‘)’], [11, ‘]’]] as a result

Read more here: Source link