问题
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?
- Min Value : 0.00
- Max Value :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.
回答1:
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.
回答2:
I guess this should help you. This regex will match 0.00
to 100,000,000.00
upto 2 decimal places.
^(:?(?=[1])(10{0,8})|(?=[^0])(\d{1,8})|0)\.[0-9]{1,2}$
But keep in mind that its always best to compare numbers numerically that using regex.
Here is the link to verify it.
来源:https://stackoverflow.com/questions/26958458/can-we-validate-min-and-max-value-for-a-floating-number-using-regexp