我有以下正则表达式:
(.*)(?:([\+\-\*\/])(-?\d+(?:\.\d+)?))
意图是以形式(左表达式)(运算符)(右操作数)捕获数学表达式,例如, 1+2+3
将被捕获为(1+2)(+)(3)
。它还将处理单个操作数,例如1+2
将被捕获为(1)(+)(2)。
我遇到的问题是这个正则表达式在没有运算符的单个操作数上不匹配,例如5应该在第一个捕获组中匹配,在第二个和第三个(5)()()中没有任何内容。如果我将最后一部分作为可选项:
(.*)(?:([\+\-\*\/])(-?\d+(?:\.\d+)?))?
然后初始组将始终捕获整个表达式。有什么方法可以让第二部分成为可选的,但是它优先于第一组的贪婪匹配吗?
答案 0 :(得分:1)
这个正则表达式将:
---
- hosts: webservers
remote_user: root
tasks:
- copy: src=/home/bu/Bilal/site dest=/tmp owner=root group=root mode=777
或1+2
或1+2+3
或1+2+3+4
原始正则表达式
注意这是Java,你需要在这个正则表达式中转义反斜杠。要转义它们,只需将所有1+2+3+4...
替换为\
。
\\
<强>概述强>
在这个表达式中,我首先验证字符串仅由操作^(?=(?:[-+*/^]?[-+]?\d+(?:[.]\d+)?)*$)([-+]?[0-9.]+$|[-+]?[0-9.]+(?:[-+*/^][-+]?[0-9.]+)*(?=[-+*/^]))(?:([-+*/^])([-+]?[0-9.]+))?$
,可选符号-+/*^
以及整数或非整数组成。由于已经验证,表达式的其余部分可以简单地将数字称为-+
,从而提高了可读性。
捕获论坛
0获取整个字符串 1获取整个字符串,但不包括最后一个操作,如果没有操作,则组1将包含整个字符串 2获取最后一个操作(如果存在) 3获取上一次操作后的数字和符号
[0-9.]+
示例文字
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
[-+*/^]? any character of: '-', '+', '*', '/',
'^' (optional (matching the most
amount possible))
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
[-+*/^] any character of: '-', '+', '*', '/',
'^'
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1
or more times (matching the most
amount possible))
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[-+*/^] any character of: '-', '+', '*', '/',
'^'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[-+*/^] any character of: '-', '+', '*', '/',
'^'
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1
or more times (matching the most
amount possible))
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
样本捕获组
1+2+-3
示例文字
[0] = 1+2+-3
[1] = 1+2
[2] = +
[3] = -3
样本捕获组
-3
示例Java代码
[0] = -3
[1] = -3
[2] =
[3] =