简体   繁体   English

智能数学正则表达式

[英]Smart math regular expression

I know there are smart ppl out there. 我知道那里有聪明的人。 At the moment, I want to use one single and the most elegant regular expression to separate numbers to achieve some really simple math calculation. 目前,我想使用一个单一且最优雅的正则表达式来分隔数字,以实现一些非常简单的数学计算。

It needs support some fuzzy number guessing for example: 它需要支持一些模糊数字猜测,例如:

1+.2 should equiv to 1 + 0.2 1 + .2应该等于1 + 0.2

.1-.2 should equiv to 0.1 - 0.2 .1-.2应等于0.1-0.2

-.2+-100.2 should equiv to -0.2 - 100.2 -.2 + -100.2应等于-0.2-100.2

Here is a quick demo I made up for you to play around http://jsfiddle.net/av57A/60/

Is that possible to achieve those logic in one elegant regular expression? 是否可以用一种优雅的正则表达式实现这些逻辑?

Thanks. 谢谢。

I got it. 我知道了。 Nice practice in regex for me. 对我来说,正则表达式的好习惯。 Also thanks for showing me jsfiddle. 也感谢您向我展示jsfiddle。 Never seen anything like that b4. 从来没有见过像b4这样的东西。


reg1 : ^[\-\d{1,}|\-*\.*\d*+]{1,}
reg2 (g) : \.{2}
reg3 (g) : ^(([\-]{0,})(\d*\.*\d*)?)|([\+\-\*\/]{1,})|((\d*)+(\.*\d*)?)

---
2-1  :  2 | - | 1
.1+.2  :  0.1 | + | 0.2
.2+1  :  0.2 | + | 1
-1-1.3  :  -1 | - | 1.3
2+1  :  2 | + | 1
-2+1  :  -2 | + | 1
2+-1  :  2 | + | -1
1+1.1  :  1 | + | 1.1
2.1+1  :  2.1 | + | 1
-1.0-1.3  :  -1 | - | 1.3
-1-.3  :  -1 | - | 0.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM