[英]Parse math expression using regular expression
I want to parse a math expression using regular expression. 我想使用正则表达式解析数学表达式。 For instance, the expression "-4-2-1" is parsed into "-4", "-", "2", "-" and "1".
例如,将表达式“ -4-2-1”解析为“ -4”,“-”,“ 2”,“-”和“ 1”。 However, from my regex, I can only get "-", "4", "-", "2", "-", "1"
但是,从我的正则表达式中,我只能得到“-”,“ 4”,“-”,“ 2”,“-”,“ 1”
this is my regex ^-?\\d*\\.{0,1}\\d+$|[+-×÷^√∛!πe%]
这是我的正则表达式
^-?\\d*\\.{0,1}\\d+$|[+-×÷^√∛!πe%]
You can't create regex for all kind of equations, but for your case you can improve your regex by 您不能为所有等式创建正则表达式,但是对于您的情况,可以通过以下方式改进正则表达式
^-
part optional, ^-
部分设为可选, -
in your character class (otherwise it will be treated as range operator) -
在您的角色类中(否则将被视为范围运算符) BTW {0,1}
can be replaced with ?
BTW
{0,1}
可以替换为?
. 。
So try with "(^-)?\\\\d*\\\\.?\\\\d+|[+\\\\-×÷^√∛!πe%]"
因此,尝试使用
"(^-)?\\\\d*\\\\.?\\\\d+|[+\\\\-×÷^√∛!πe%]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.