Java 在符号前后捕获数字的正则表达式
我希望能够发送一个数学表达式/方程到我的程序中,并让它给我结果的答案。我正在使用正则表达式,来解码要操作的数字。这是我当前添加的正则表达式代码:Java 在符号前后捕获数字的正则表达式,java,regex,numbers,capturing-group,Java,Regex,Numbers,Capturing Group,我希望能够发送一个数学表达式/方程到我的程序中,并让它给我结果的答案。我正在使用正则表达式,来解码要操作的数字。这是我当前添加的正则表达式代码: Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\+\\s*(\\d+).*").matcher(e); if(!m.matches()) return e; e = ((int) Addition.add(Double.parseDouble(m.group(1)), Double.parseDoub
Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\+\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
e = ((int) Addition.add(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
这适用于m.group(2)
中的第二个数字,但m.group(1)
只返回运算符符号前的最后一个数字,我尝试了几种不同的方法,这是我最新的一种,没有运气。我做错了什么
编辑:这是3种解决方法,它有我测试的一些测试输出
public double solve(String e) {
if (!validExpression(e))
return 0;
e = e.replace(" ", "");
boolean doPar = false;
if (e.contains("(") || e.contains(")")) {
doPar = true;
int par = 0;
for (char c : e.toCharArray()) {
if (c == '(')
par++;
else if (c == ')')
par--;
}
if (par != 0)
return 0;
}
if (doPar)
e = solveParentheses(e);
e = solveSmallExpression(e);
return Double.parseDouble(e.replace("(", "").replace(")", ""));
}
private static String solveSmallExpression(String e) {
System.out.println(e);
while(e.contains("^")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\^\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Exponentiation.raise(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
while(e.contains("/") || e.contains("*")) {
if(e.contains("/")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*/\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Division.divide(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
if(e.contains("*")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,}+)\\s*\\*\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Multiplication.multiply(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
}
while(e.contains("-") || e.contains("+")) {
if(e.contains("-")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\-\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Subtraction.subtract(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
if(e.contains("+")){
final Matcher m = Pattern.compile(".*\\D*(\\d{1,})\\s*\\+\\s*(\\d+).*").matcher(e);
if(!m.matches()) return e;
System.out.println(m.group(1) + "\t" + m.group(2));
e = ((int) Addition.add(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))) + "";
}
}
System.out.println(e);
System.out.println();
return e;
}
private static String solveParentheses(String e) {
while (e.contains("(") && e.contains(")")) {
int start = -1;
int end = -1;
for(int i = 0; i < e.length(); i++) {
char c = e.charAt(i);
if(c == '(')
start = i + 1;
else if(c == ')')
end = i;
}
String sub = e.substring(start, end);
if(sub.contains("(") && sub.contains(")"))
sub = solveParentheses(sub);
sub = solveSmallExpression(sub);
e = e.replace(e.subSequence(start - 1, end + 1), sub);
}
return e;
}
private static boolean validExpression(String e) {
for (char c : e.toCharArray()) {
if (!Character.isDigit(c) && c != '(' && c != ')' && c != '^' && c != '+' && c != '-' && c != '/' && c != '*' && c != ' ')
return false;
}
return true;
}
*贪婪,会吃掉整个字符串,然后回滚,直到它碰到匹配的东西。因为一个非数字(\D*)可以出现0次,并且至少需要一个数字和加号,所以当正则表达式回滚到足以达到该值时,它将匹配
您可以通过使用“*?”来解决这个问题,而不是使用一个不情愿的运算符,该运算符可以获取尽可能少的字符。为什么您不在两个组中使用相同的组删除
表示使用(\d+)请提供一些示例输入和预期输出。“数学表达式”过于通用。是否要支持括号?模数?对数?或者仅仅是
+-*/
?对于一般的数学表达式解析,您应该实现一个解析器,比如It worked!我不知道你能用?
阻止贪婪的操作员。我知道它存在,但直到现在我才完全知道它做了什么。谢谢
24/12
4 12
0
24*100
4 100
400
400-0
0 0
0
0.0