简体   繁体   English

提取两个括号之间的字符串,包括python中的嵌套括号

[英]Extract string between two brackets, including nested brackets in python

How to extract string between two brackets, including the nested brackets. 如何在两个括号(包括嵌套的括号)之间提取字符串。

There is a string: 有一个字符串:

""res = sqr(if((a>b)&(a<c),(a+b)*c,(a-b)*c)+if()+if()...)""

How can I extract the all the contents of if() as following: 我如何提取if()的所有内容,如下所示:

["if((a>b)&(a<c),(a+b)*c,(a-b)*c)","if()","if()",...]

The format is not fixed, the string may includes multi- if s. 格式不是固定的,字符串可以包含multi- if So I want to know if there is a pattern that can match the substring. 所以我想知道是否有可以匹配子字符串的模式。 I'll try to give my solution later. 稍后我将尝试给出解决方案。 thanks. 谢谢。

My solution, if there is any better method, please point out to me: 我的解决方案,如果有更好的方法,请向我指出:

def extractIfFunc(condStr):

startIndex = [m.start() for m in re.finditer('if\(',condStr)]
parts = []
for index in startIndex:
    current = []
    bracket_level = 0
    for s in condStr[index+3:]:
        if s != '(' and s != ')' and bracket_level >= 0:
            current.append(s)
        elif s == '(':
            current.append(s)
            bracket_level += 1
        elif s == ')':
            bracket_level -= 1 
            if bracket_level < 0:
                current.append(s)
                break
            else:
                current.append(s)     
    parts.append('if('+''.join(current))
return parts  

尝试这个:

st[st.find('(')+1:st.rfind(')')]
>>> import re
>>> s = """res = sqr(if((a>b)&(a<c),(a+b)*c,(a-b)*c)+if()+if()...)"""
>>> re.findall(r'if\((?:[^()]*|\([^()]*\))*\)', s)
['if((a>b)&(a<c),(a+b)*c,(a-b)*c)', 'if()', 'if()']

For such patterns, better to use VERBOSE flag: 对于这种模式,最好使用VERBOSE标志:

>>> lvl2 = re.compile('''
...          if\(            #literal if(
...            (?:           #start of non-capturing group
...             [^()]*       #non-parentheses characters
...             |            #OR
...             \([^()]*\)   #non-nested pair of parentheses
...            )*            #end of non-capturing group, 0 or more times
...          \)              #literal )
...          ''', flags=re.X)
>>> re.findall(lvl2, s)
['if((a>b)&(a<c),(a+b)*c,(a-b)*c)', 'if()', 'if()']


To match any number of nested pairs, you can use regex module, see Recursive Regular Expressions 要匹配任意数量的嵌套对,可以使用regex模块,请参见递归正则表达式

st = """res = sqr(if((a>b)&(a<c),(a+b)*c,(a-b)*c))"""

print(st[10:][:-1])

OUTPUT: 输出:

if((a>b)&(a<c),(a+b)*c,(a-b)*c)

EDIT: 编辑:

For a generic approach: 对于通用方法:

import re
st = """res = sqr(if((a>b)&(a<c),(a+b)*c,(a-b)*c))"""
pattern = "\((.*)\)"
print(re.compile(pattern).search(st).group(1))

Where: 哪里:

\\( matches the character ( literally (case sensitive) \\(匹配字符(按字面值(区分大小写)

1st Capturing Group (.*) 第一捕获组(.*)

.* matches any character (except for line terminators) .*匹配任何字符(行终止符除外)

\\) matches the character ) literally \\) )从字面上匹配字符)

OUTPUT: 输出:

if((a>b)&(a<c),(a+b)*c,(a-b)*c)

regexTester regexTester

def extractIfFunc(condStr):
    for i, segment in enumerate(a.split("if")):
        if i == 0:
            continue

        s, n = -1, 0
        for i, c in enumerate(segment):
            if c == '(':
                s = i if s < 0 else s
                n += 1
            elif c == ')':
                n = n - 1 if n > 0 else 0
                if n == 0 and s > -1:
                    yield "if(%s)" % segment[s + 1:i]
                    break


a = """res = sqr(if((a>b)&(a<c),(a+b)*c,(a-b)*c)+if()+if()...)"""

for segment in extractIfFunc(a):
    print(segment)

NOTE: 注意:

This is not a real parser. 这不是真正的解析器。 If you want to create a parser that matches LALR(1) grammar , maybe the PLY is what you are looking for. 如果要创建与LALR(1)语法匹配的解析器,则可能是您要查找的PLY It can help you to build a complete parser. 它可以帮助您构建完整的解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM