正则表达式:匹配括号贪婪和非贪婪

Gab*_*ira 5 python regex regex-greedy

我正在使用python正则表达式模块,re.

我需要在这两个短语中匹配'('')'内的任何内容,但"不那么贪心".像这样:

show the (name) of the (person)

calc the sqrt of (+ (* (2 4) 3))
Run Code Online (Sandbox Code Playgroud)

结果应该从短语1返回:

name
person
Run Code Online (Sandbox Code Playgroud)

结果应该从短语2返回:

+ (* (2 4) 3)
Run Code Online (Sandbox Code Playgroud)

问题是,为了适应第一个短语,我使用了 '\(.*?\)'

在第二个短语中,这恰好适合 + (* (2 4)

并使用'\(.*\)'正确拟合第二个短语,在第一个短语适合(name) of the (person)

什么正则表达式适用于这两个短语?

Pau*_*McG 6

Pyparsing可以很容易地为这样的东西编写简单的一次性解析器:

>>> text = """show the (name) of the (person)
...
... calc the sqrt of (+ (* (2 4) 3))"""
>>> import pyparsing
>>> for match in pyparsing.nestedExpr('(',')').searchString(text):
...   print match[0]
...
['name']
['person']
['+', ['*', ['2', '4'], '3']]
Run Code Online (Sandbox Code Playgroud)

请注意,嵌套的parens已被丢弃,嵌套的文本作为嵌套结构返回.

如果您想要每个括号位的原始文本,请使用originalTextFor修饰符:

>>> for match in pyparsing.originalTextFor(pyparsing.nestedExpr('(',')')).searchString(text):
...   print match[0]
...
(name)
(person)
(+ (* (2 4) 3))
Run Code Online (Sandbox Code Playgroud)