Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I used a regular expression to restrict the numbers only between 3.00 to 100.00.But it was allowing the numbers between 100.01 to 100.99

Tags:

regex

I have a requirement to restrict the numbers between 3.00 to 100.00 I used below expression

^([3-9]|[1-9][0-9]|100)+(\.\d{1,2})?$

The issue with above expression is that, it's allowing 100.01 to 100.99,which should be restricted.It also allows 310 to 399,which needs to restricted.

I used another flavor of same expression

^([3-9]|[1-9][0-9]|100.00)+(\.\d{1,2})?$

Which was working as expected,but we need to enter 100.00 in to pass the regular expression instead of 100.

Is there any way,I can achieve the desired result?

like image 725
Muralikrishna Avatar asked Nov 19 '25 11:11

Muralikrishna


1 Answers

When alternating with the final 100, use negative lookahead for \.\d?[1-9], to ensure that the decimal places, if any, have only 0s.

Your first pattern can also match many repeated digits before the optional decimal (like 333 and 101010) due to the + at the end of the group, so best to remove the + if you only want to match between 3 and 100.

^(?:[3-9]|[1-9][0-9]|100(?!\.\d?[1-9]))(?:\.\d{1,2})?$
                        ^^^^^^^^^^^^^^

https://regex101.com/r/tJd3LQ/1

To permit leading zeros, add 0* right after the ^:

^0*(?:[3-9]|[1-9][0-9]|100(?!\.\d?[1-9]))(?:\.\d{1,2})?$
 ^^
like image 94
CertainPerformance Avatar answered Nov 21 '25 01:11

CertainPerformance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!