Home > Software engineering >  Regex splitting with multiple end brackets
Regex splitting with multiple end brackets

Time:12-21

I have an input of 10 sqrt(10 (100*20) 20) sqrt(5) which I need to be able to split up into sqrt(...) as many times as sqrt appears (in this instance, twice). The problem I am having is trying to split this up, I have tried on my own and come up with this (sqrt\()(?<=sqrt\()(. ?)(\) ) but regex only registers the first ) it comes across whereas I need it to find the closing bracket.

My REGEX attempt

As you can see in the picture the orange marker only covers up to the first bracket but i need it to end at the 20.

The desired output is a list as follows:

['10 ', 'sqrt(', '10 (100*20) 20', ')', ' ', 'sqrt(', '5', ')']

Thanks in advance

CodePudding user response:

Here is a solution using the ast module from the standard library. I'm assuming the expression is valid Python.

# Import parser from Python standard library
import ast

# I have an expression to process
expression = '10 sqrt(10 (100*20) 20) sqrt(5)'

# Parse it
tree = ast.parse(expression)

# Walk the parse tree
for node in ast.walk(tree):
    # Are we a function call?
    if isinstance(node, ast.Call):
        func = node.func
        # Are we a call to 'sqrt'?
        if expression[func.col_offset:func.end_col_offset] == 'sqrt':
            # We have what we want
            start = node.col_offset
            end = node.end_col_offset
            print(expression[start:end], start, end)

This outputs...

sqrt(5) 24 31
sqrt(10 (100*20) 20) 3 23

CodePudding user response:

You don't need to seperate the string. If you need to execute and return the result then, we can just use eval() to do it.


from math import *

query = "10 sqrt(10 (100*20) 20) sqrt(5)"

print(eval(query))

# Output: 57.29158928177503

But if you need to just split the equation, we can use classic split(). Reason: It's easier to manage if we don't know no. of sqrt() in equation.

s = "10 sqrt(10 (100*20) 20) sqrt(5)"

l = s.split('sqrt(')

d = [l[0]]

x = ''

for i in range(1, len(l)):

    if x: d.append(x)

    d.append('sqrt(')

    q = l[i]

    if i 1 == len(l): d.append(q)

    else:

        d.append(q[:-1])

        x = q[-1]



print(d)

# Output: ['10 ', 'sqrt(', '10 (100*20) 20)', ' ', 'sqrt(', '5)']

Peace!

  • Related