Reputation: 187
I am trying to find text contained within the outermost brackets of a function, filt().
The function is part of a math expression string:
math_expr = "filt(2*A) + filt(A*(B+C)) - filt((A+B)/(C+D))"
The expected output is:
['2*A', 'A*(B+C)', '(A+B)/(C+D)']
I have tried using several regex expressions on this site, the closest output I got was by using re.findall('\((.*?)\)', math_expr)
. However, the regex returns:
['2*A', 'A*(B+C', '(A+B', 'C+D']
Could someone please help me? I am new to regex and don't know what else to try. Thank you!
Upvotes: 1
Views: 670
Reputation: 482
Just as a really easy alternative
>>> import re
>>> math_expr = "filt(2*A) + filt(A*(B+C)) - filt((A+B)/(C+D))"
>>> re.findall(r'\(([\S]*?)\)(?=\s|$)', math_expr)
#OUTPUT
['2*A', 'A*(B+C)', '(A+B)/(C+D)']
Upvotes: 1
Reputation: 3419
I have coded it up using a stack approach. It just puts in (
into a stack and then pops from it when you see a )
.
math_expr = "filt(2*A) + filt(A*(B+C)) - filt((A+B)/(C+D))"
stack = []
index = 0
overallresult = []
while (index < len(math_expr)):
if math_expr[index] == '(':
stack.append('(')
result = ''
index +=1
while(index<len(math_expr) and len(stack)>0):
result += math_expr[index]
if(math_expr[index]=='('):
stack.append('(')
elif (math_expr[index]==')'):
stack.pop()
index+=1
overallresult.append(result[:-1])
index+=1
print(overallresult)
OUTPUT
['2*A', 'A*(B+C)', '(A+B)/(C+D)']
Upvotes: 0
Reputation: 370689
If you use the regex module, which supports recursive patterns, you can use
regex.findall(r'\(((?:[^()]+|(?R))+)\)', math_expr)
Output:
['2*A', 'A*(B+C)', '(A+B)/(C+D)']
https://regex101.com/r/oclWxx/1
\(
- Leading parentheses ((?:[^()]+|(?R))+)
- Capturing group, which repeatedly matches:
[^()]+
- Anything but parentheses, or(?R)
- The entire pattern, recursed again\)
- Trailing parenthesesUpvotes: 2