Approach using split and for loop
Here's another approach to get this done. In this approach, I am splitting the string into parts.
Splitting them by left parenthesis and right parenthesis.
Then concatenating them each time to create the expression
Assumption: The expression has equal number of left and right parenthesis
- Step 1: Count the number of left parenthesis in the string
- Step 2: Split the expression by left parenthesis
- Step 3: pop the last expression from the list of left parenthesis and
store into right expression. This contains right parenthesis
- Step 4: Split the expression by right parenthesis
- Step 5: Now that you have both the sides, stitch them together
- Note: While concatenating the expression, left side goes from right
to left (index -1 thru 0) and right side goes from left to right
(index 0 to -1)
- Note: For each iteration, you need to concatenate the previous answer
with left and right
Code is as shown below:
expression = "LEN(Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai'))"
n = expression.count('(')
exp_left = expression.split('(')
exp_right = exp_left.pop().split(')')
exp_list = []
exp_string = ''
for i in range(n):
exp_string = exp_left[-i-1] + '(' + exp_string + exp_right[i] + ')'
exp_list.append(exp_string)
for exp in exp_list: print (exp)
The output of this will be:
UPPER([ProductName]+[ProductName])
Lower(UPPER([ProductName]+[ProductName]))
Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai')
LEN(Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai'))
Below code is the same as above. I have added comments to each line for you to understand what's being done.
expression = "LEN(Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai'))"
#find the number of equations in the string. Count of '(' will give you the number
n = expression.count('(')
#split the string by left parenthesis. You get all the functions + middle part + right hand side
exp_left = expression.split('(')
#middle + right hand part is at position index -1. Use pop to remove the last value
#Use the popped string to split by right parenthesis
#result will be middle part + all right hand side parts.
#empty string if there was no text between two right parenthesis
exp_right = exp_left.pop().split(')')
#define a list to store all the expressions
exp_list = []
#Now put it all together looping thru n times
#store the expression in a string so you can concat left and right to it each time
exp_string = ''
for i in range(n):
#for each iteration, concat left side + ( + middle string + right side + )
#left hand side: concat from right to left (-1 to 0)
#right hand side: concat from left to right (0 to n-1)
exp_string = exp_left[-i-1] + '(' + exp_string + exp_right[i] + ')'
#append the expression to the expression list
exp_list.append(exp_string)
#print each string separately
for exp in exp_list: print (exp)
Approach using While Statement
Here's how to do the search and extract version.
e = "LEN(Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai'))"
x = e.count('(')
for i in range(x-1): e = e[e.find('(')+1:]
expression = e[:e.find(')')+1]
print (expression)
The result of this will be:
UPPER([ProductName]+[ProductName])
If you want all of them, then you can do this until you reach the innermost brackets.
e = "LEN(Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai'))"
#x = e.count('(')
#for i in range(x-1): e = e[e.find('(')+1:]
#expression = e[:e.find(')')+1]
exp_list = [e]
while e.count('(') > 1:
e = e[e.find('(')+1:e.rfind(')')]
while e[-1] != ')': e = e[:e.rfind(')')+1]
exp_list.append(e)
for exp in exp_list:
print (exp)
The output of this will be:
LEN(Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai'))
Replace(Lower(UPPER([ProductName]+[ProductName])),'chaichai','chai')
Lower(UPPER([ProductName]+[ProductName]))
UPPER([ProductName]+[ProductName])