带小数点和减号的计算器正则表达式 [英] Calculator Regular Expression with decimal point and minus sign

查看:100
本文介绍了带小数点和减号的计算器正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式来验证以下输入:

I need a regular expression that validates the following inputs:

-0.12-12.56
-0.12+12.56
-0.12*12.56
-0.12/12.56
12.3--12.56
12.3+-12.56
12.3*-12.56
12.3/-12.56
-0.12--12.56
-0.12+-12.56
-0.12*-12.56
-0.12/-12.56
0.12-12.56
0.12+12.56
0.12*12.56
0.12/12.56

此外,它还应允许所有这些条件不带小数点,并且如果允许小数点,则不允许重复一次,即在数字之间或在操作数开头.

Also it should allow all this conditions without decimal point and if decimal point is allowed then it should not be allowed more than once i.e. either between the digits or at start of an operand.

数字前的减号也应该是可选的.

The minus sign should also be optional before the number.

当前我正在使用以下正则表达式:

Currently I am using below regular expression:

^-{0,1}[0-9.]+[+-×÷]{0,1}-{0,1}[0-9.]*$

任何帮助将不胜感激!!!

Any help would be greatly appreciated !!!

推荐答案

您可以尝试使用以下正则表达式:

You can try with the following regex:

/^-?\d+\.?\d+[-+*\/]-?\d+\.?\d+$/mg

您可以在这里尝试: http://refiddle.com/refiddles/57ea735f75622d0630d13500

这篇关于带小数点和减号的计算器正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆