与这个问题类似,我试图将一个字符串与*两个可选值进行匹配,这两个值都用括号括起来,长度未知。有关字符串的示例如下:
4
4(2(3))
4(2(3)(1))
4(2(3)(1))(6(5))
1(2(3(4(5(6(7(8)))))))
-4(2(3)(1))(6(5))
始终有一个数字可能是负数,括号中最多有2项。我希望将其分组,因此对于最后一项,分组为:
group 1: -4
group 2: 2(3)(1)
group 3: 6(5)
向正则表达式中添加另一个可选组似乎有问题:
(-?\d+)(?:\((.*)?\))?(?:\((.*)?\))?
^ ^ ^
group 1 group 2 group 3
使用
^(-?\d+)(?:\((\d+(?:\(\d+\))*)?\))?(?:\((\d+(?:\(\d+\))*)?\))?$
见证明。
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
-? '-' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
( group and capture to \2 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\) ')'
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
)? end of \2 (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in \2)
--------------------------------------------------------------------------------
\) ')'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
( group and capture to \3 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\) ')'
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
)? end of \3 (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in \3)
--------------------------------------------------------------------------------
\) ')'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
您可以尝试使用regex演示来捕获这些组:
表达式:(-?\d)(\(\d*\(\d*\)\(\d*\)\)|(?:\(\d*\(\d*\)\)))?(\(\d*\(\d*\)\))?
细节:最后两组是可选组,第二组是交替。