简体   繁体   English

Python正则表达式匹配括号,但不嵌套括号

[英]Python Regex match parenthesis but not nested parenthesis

Is it possible to match parenthesis like () but not allowing nesting? 是否可以匹配()之类的括号但不允许嵌套? In other words, I want my regex to match () but not (()) The regex that I am trying is 换句话说,我希望我的正则表达式匹配()而不是(())我正在尝试的正则表达式是

\(\[^\(\)])

but it does not seem to be working. 但它似乎不起作用。 Can someone explain to me what I'm doing wrong? 有人可以向我解释我在做什么错吗?

If (foo) in x(foo)x shall be matched, but (foo) in ((foo)) not, what you want is not possible with regular expressions, as regular expressions represent regular grammars and all regular grammars are context free . 如果(foo)x(foo)x须匹配,但是(foo)((foo))不是,你想要什么是不可能的正则表达式,正则表达式表示正则文法和所有正规文法都是上下文 But context (or 'state', as Jonathon Reinhart called it in his comment ) is necessary for the distinction between the (foo) substrings in x(foo)x and ((foo)) . 但是要区分x(foo)x((foo))(foo)子字符串,必须使用上下文(或“状态”,如Jonathon Reinhart在其评论中所称的 ((foo))

If you only want to match strings that only consist of a parenthesized substring, without any parentheses (matched or unmatched) in that substring, the following regex will do: 如果只想匹配包含括号的子字符串且该子字符串中没有任何括号(匹配或不匹配)的字符串,则以下正则表达式将起作用:

^\([^()]*\)$
  • ^ and $ 'glue' the pattern to the beginning and end of the string, respectively, thereby excluding partial matches ^$将模式分别粘贴到字符串的开头和结尾,从而排除部分匹配项
  • note the arbitrary number of repetitions (… * ) of the non-parenthesis character inside the parentheses. 请注意括号内非括号字符的任意重复数(… * )。
  • note how special characters are not escaped inside a character set, but still have their literal meaning. 注意特殊字符如何字符集中不转义,但仍具有其字面意义。 (Putting backslashes in there would put literal backslashes in the character set. Or in this case out of the character set, due to the negation.) (在其中添加反斜杠会在字符集中放置原义的反斜杠 。或者在这种情况下,由于求反,会将其放在字符集中。)
  • note how the [ starting the character set isn't escaped, because we actually want its special meaning, rather than is literal meaning 请注意如何避免[起始字符集的转义,因为我们实际上想要的是特殊含义,而不是字面意思

The last two points might be specific to the dialect of regular expressions Python uses. 最后两点可能特定于Python使用的正则表达式的方言。

So this will match () and (foo) completely, but not (not even partially) (foo)bar) , (foo(bar) , x(foo) , (foo)x or ()() . 因此,这将完全匹配()(foo) ,但不会(甚至不完全匹配) (foo)bar)(foo(bar)x(foo)(foo)x()()

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

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