Python:如何使用正则表达式匹配嵌套的括号?

26

我试图匹配一个类似于数学表达式的字符串,其中包含嵌套括号。

import re

p = re.compile('\(.+\)')
str = '(((1+0)+1)+1)'
print p.findall(s)

['(((1+0)+1)+1)']

我想让它匹配所有包含的表达式,例如(1+0), ((1+0)+1)...
即使它匹配了不想要的表达式如(((1+0),我也可以处理。

为什么它现在没有做到这一点,我该怎么做呢?


1
可能是在Python中使用正则表达式匹配嵌套结构的重复问题。 - 0 _
13个回答

0

这是一个演示,回答您的问题,虽然它有些笨拙,但它能够正常工作。

import re s = '(((1+0)+1)+1)'

def getContectWithinBraces( x , *args , **kwargs):
    ptn = r'[%(left)s]([^%(left)s%(right)s]*)[%(right)s]' %kwargs
    Res = []
    res = re.findall(ptn , x)
    while res != []:
        Res = Res + res
        xx = x.replace('(%s)' %Res[-1] , '%s')
        res = re.findall(ptn, xx)
        print(res)
        if res != []:
            res[0] = res[0] %('(%s)' %Res[-1])
    return Res

getContectWithinBraces(s , left='\(\[\{' , right = '\)\]\}')

-1
你应该编写一个适当的解析器来解析这样的表达式(例如使用pyparsing)。正则表达式不是编写良好解析器的合适工具。

我认真听取了你的回答,并写下了我谨慎地称之为“我的第一个Pyparsing语法”的内容。 - phooji

-2
许多帖子建议对于嵌套的大括号,正则表达式不是解决方法。简单地计算大括号数量即可: 例如,请参见:Regular expression to detect semi-colon terminated C++ for & while loops 这里是一个完整的Python示例,用于迭代字符串并计算大括号数量:
# decided for nested braces to not use regex but brace-counting
import re, string

texta = r'''
nonexistent.\note{Richard Dawkins, \textit{Unweaving the Rainbow: Science, Delusion
and the Appetite for Wonder} (Boston: Houghton Mifflin Co., 1998), pp. 302, 304,
306-309.} more text and more.

 This is a statistical fact, not a
guess.\note{Zheng Wu, \textit{Cohabitation: An Alternative Form
of Family Living} (Ontario, Canada: Oxford University Press,
2000), p. 149; \hbox{Judith} Treas and Deirdre Giesen, ``Title
and another title,''
\textit{Journal of Marriage and the Family}, February 2000,
p.\,51}

more and more text.capitalize
'''
pos = 0
foundpos = 0
openBr = 0 # count open braces
while foundpos <> -1:
    openBr = 0
    foundpos = string.find(texta, r'\note',pos)
    # print 'foundpos',foundpos
    pos = foundpos + 5
    # print texta[pos]
    result = ""
    while foundpos > -1 and openBr >= 0:
        pos = pos + 1
        if texta[pos] == "{":
            openBr = openBr + 1
        if texta[pos] == "}":
            openBr = openBr - 1
        result = result + texta[pos]
    result = result[:-1] # drop the last } found.
    result = string.replace(result,'\n', ' ') # replace new line with space
    print result

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接