在python中查找内部偏执字符串和外部偏执字符串

在python中查找内部偏执字符串和外部偏执字符串,python,Python,假设我有一个字符串输入 str=Length(name, concat(name, concat(name, name) ) ) 我需要从内心深处获得输出,比如: concat(name, name) concat(name, concat(name, name) ) Length(name, concat(name, concat(name, name) ) ) 我在代码中尝试了下面的内容,但没有给出正确的结果。函数名不必是concat,也可以是any。我只尝试了concat。请

假设我有一个字符串输入

str=Length(name, concat(name,  concat(name, name) )  )
我需要从内心深处获得输出,比如:

concat(name, name)
concat(name,  concat(name, name) )
Length(name, concat(name,  concat(name, name) )  )
我在代码中尝试了下面的内容,但没有给出正确的结果。函数名不必是concat,也可以是any。我只尝试了concat。请帮我解决这个问题

matches = re.finditer('concat',str)
positions = [match.start() for match in matches]
positions.sort(reverse=True)
print(positions)
for i in positions:
    out_expr = re.compile("\((.+)\)")
    res = re.search(out_expr,str[i:])
    inner = res.group(1)
    print(inner)

从您的描述中不清楚如何解析括号前的单词,但是正则表达式不太可能是正确的解决方案

您可能需要基于括号解析一个简单的树,例如

ex_str = "Length(name, concat(name,  concat(name, name) )  )"

def parse_paranthesis_tree(string):
    """
    Parses a tree containing the use of parenthesis within a string.
    """

    depth = 0
    active_node_per_depth = {}
    root_node = None
    for pos,char in enumerate(string):
        if char == '(':
            cur_node = {'start' : pos,
                    'end' : -1,
                    'depth': depth,
                    'children' : []}
            
            active_node_per_depth[depth] = cur_node
            if depth > 0:
                active_node_per_depth[depth-1]\
                        ['children'].append(cur_node)
            else:
                root_node = cur_node
            depth += 1

        if char == ')':
            active_node_per_depth[depth-1]['end'] = pos
            active_node_per_depth[depth-1] = None
            depth -= 1
    return root_node

def print_paranthesis_tree(node,string):
    print (string[node['start']:node['end']+1])
    for child_node in node['children']:
        print_paranthesis_tree(child_node,string)

print_paranthesis_tree(parse_paranthesis_tree(ex_str),ex_str)
这个输出

(name, concat(name,  concat(name, name) )  )
(name,  concat(name, name) )
(name, name)

请注意,如果您在解析单词时提供了清晰的语法或规则集(例如,是否应忽略空白?),则可以轻松扩展此功能以解析括号前的单词。我使用了find函数。看看这是否适合你

import re
a='Length(name, concat(name, concat(name, name) ) )'
matches = re.finditer('concat',a)
positions = [match.start() for match in matches]
positions.sort(reverse=True)
li = []
x = 0
for i in positions:
    x = a.find(')',x) + 1
    li.append(a[i:x])
li.append(a)

for c in li: print (c)
输出为:

concat(name, name)
concat(name, concat(name, name) )
Length(name, concat(name, concat(name, name) ) )
我进一步修改了代码。这里有一个更新版本。让我知道这是否适合你。 假设:外圆括号之间应该有一个空格

import re
a='Length(name, concat(name, concat(name, name) ) )'
start_pos=0
end_pos = len(a) - 2
matches = re.finditer('\(',a)
positions = [match.start() for match in matches]
positions.pop(0)
print(a)
for i in positions:
    start_pos = a[:i].rfind(',') + 2
    print(a[start_pos: end_pos])
    end_pos -= 2
输出与前面的相同:

concat(name, name)
concat(name, concat(name, name) )
Length(name, concat(name, concat(name, name) ) )

谢谢你的回复。我只需要知道字符串中使用了哪些函数(如concat、Length等),然后用我在回答中发布的参数单独检索该函数。你没有给出语法规则,说明什么是要匹配的有效“函数”名称。如果没有这个定义,回答你的问题是不可能的。谢谢你的回答。这将只适用于concat函数。我想要所有类型的函数@Joe FerndzLet让我知道新代码是否适用于你。这仍然不是最好的,但它应该为你提供一些方向。