[英]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 ^
和$
将模式分别粘贴到字符串的开头和结尾,从而排除部分匹配项 *
) of the non-parenthesis character inside the parentheses. *
)。 [
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.