说明
此正则表达式将:
- 捕获直到最后一个操作的数学表达式
- 捕获最后一次操作
- 捕获数学表达式中的最后一个数字
- 假设每个数字可能有一个加号或减号表示该数字是正数还是负数
- 假设每个数字可能是非整数
- 假设数学表达式可以包含任意数量的运算,例如:
1+2 或 1+2+3 或 1+2+3+4 或 1+2+3+4...
- 验证字符串是否为数学表达式。这里没有考虑一些边缘情况,例如使用括号或其他复杂的数学符号。
原始正则表达式
注意这是 Java,您需要转义此正则表达式中的反斜杠。要逃避它们,只需将所有 \ 替换为 \\。
^(?=(?:[-+*/^]?[-+]?\d+(?:[.]\d+)?)*$)([-+]?[0-9.]+$|[-+]?[0-9.]+(?:[-+*/^][-+]?[0-9.]+)*(?=[-+*/^]))(?:([-+*/^])([-+]?[0-9.]+))?$
说明
概述
在这个表达式中,我首先验证字符串是否仅由操作 -+/*^、可选符号 -+ 以及整数或非整数数字组成。由于已经过验证,表达式的其余部分可以简单地将数字称为[0-9.]+,这提高了可读性。
捕获组
0 获取整个字符串
1 获取整个字符串,但不包括最后一个操作,如果没有操作,则第 1 组将拥有整个字符串
2 获取最后一个操作,如果存在的话
3 获取最后一次操作后的数字和符号
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
在线演示:http://fiddle.re/b2w5wa
示例文本
-3
样本捕获组
[0] = -3
[1] = -3
[2] =
[3] =
在线演示:http://fiddle.re/07kqra
Java 代码示例
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "source string to match with pattern";
Pattern re = Pattern.compile("^(?=(?:[-+*/^]?[-+]?\\d+(?:[.]\\d+)?)*$)([-+]?[0-9.]+$|[-+]?[0-9.]+(?:[-+*/^][-+]?[0-9.]+)*(?=[-+*/^]))(?:([-+*/^])([-+]?[0-9.]+))?$",Pattern.CASE_INSENSITIVE);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}