使用正则表达式验证数学表达式? [英] Validate mathematical expressions using regular expression?
问题描述
我想使用正则表达式验证数学表达式.数学表达式可以是这个
I want to validate mathematical expressions using regular expression. The mathematical expression can be this
可以为空表示什么都没有输入
It can be blank means nothing is entered
如果指定,它将始终以运算符 +
或 -
或 *
或 /
开头并且总是后跟一个可以有的数字任意数量的数字,数字可以是十进制(在数字之间包含 .
)或整数(数字中没有."符号).示例: *0.9
, +22.36
, - 90
, /0.36365
If specified it will always start with an operator +
or -
or *
or /
and will always be followed by a number that can have
any number of digits and the number can be decimal(contains .
in between the numbers) or integer(no '.' symbol within the number).
examples : *0.9
, +22.36
, - 90
, / 0.36365
然后可以跟在第 2 点(上面的行)中提到的内容.例子: *0.9+5
, +22.36*4/56.33
, -90+87.25/22
, /0.36365/4+2.33
It can be then followed by what is mentioned in point 2 (above line).
examples : *0.9+5
, +22.36*4/56.33
, -90+87.25/22
, /0.36365/4+2.33
请帮帮我.
推荐答案
这样的事情应该可行:
^([-+/*]\d+(\.\d+)?)*
^
- 字符串的开头[-+/*]
- 这些运算符之一\d+
- 一个或多个数字(\.\d+)?
- 一个可选的点,后跟一个或多个数字()*
- 整个表达式重复零次或多次
^
- beginning of the string[-+/*]
- one of these operators\d+
- one or more numbers(\.\d+)?
- an optional dot followed by one or more numbers()*
- the whole expression repeated zero or more times
这篇关于使用正则表达式验证数学表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!