我们可以使用RegExp验证浮点数的最小值和最大值吗? [英] Can we validate Min and Max Value for a floating Number using RegExp?

查看:88
本文介绍了我们可以使用RegExp验证浮点数的最小值和最大值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以使用Regex验证数字的最小和最大长度. 但是我们可以使用相同的值来验证最小和最大值浮点数吗?

Hi I know that we can validate Min and Max Length of a number using Regex. But can we validate Min and Max Value for a floating point number using the same?

  • 最小值:0.00
  • 最大值:100,000,000.00

任何人都可以将最小值和最大值"应用于以下正则表达式:

Could anyone please just apply Min and Max Value to following Regex:

^(?=.*\d)(?!.*?\.[^.\n]*,)\d*(,\d*,?)*(\.\d*)?$

正则表达式上方的浮点数与可选的小数点和逗号匹配.

Above Regex matches a floating number with optional decimal point and commas.

推荐答案

正则表达式用于字符串.您尝试比较浮点数.这不是正确的工具.这比用叉子吃汤还差.这就像用刀在纸上书写或用茶匙剪头发一样.

Regex is for strings. You try to compare floats. It's just not the right tool. It's worse than eating your soup with a fork. It's like writing on paper with a knife or cutting your hair with a teaspoon.

在此处查找具有正整数且不带千位分隔符的解决方案: 使用正则表达式比较数字

Look here for a solution with positive integers without thousands separator : Using regular expressions to compare numbers

我剩下的任务是将解决方案扩展到使用浮点数,数千个分隔符和负数.

I leave the task to you to extend that solution to using floats, thousands separator and negative numbers.

这篇关于我们可以使用RegExp验证浮点数的最小值和最大值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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