user45268
user45268

Reputation: 23

Regex with decimal number that must be specific number

I need a regex for numbers like those

+2.25
-9,75
+02,50
-10.00

What I have done so far is this ^([-+]\d{2}[.,][00|25|50|75]{1,2})$

1- [-+] = obligatory at the beginning
2- \d{2} = any number between 0 and 99
3- [.,] = separator can be .or,
4- [00|25|50|75]{1,2} = input must be 00 or 25 or 50 or 75

The number 4- is not working as you can test here https://regex101.com/.

What I want and what I don't want as results

-9.75 Good
-9.77 Bad

the end must always be 00 or 25 or 50 or 75

Upvotes: 0

Views: 37

Answers (1)

Ariel
Ariel

Reputation: 1436

You need to accept 1 or 2 numbers first.

^[-+]\d{1,2}[.,](00|25|50|75)$

the only modification to your regex: \d{1,2}, it accepts one or two digits.

Another option:

^[-+]\d?\d[.,](00|25|50|75)$

\d?\d makes the first digit optional.

You can test it here and here

Upvotes: 2

Related Questions