Regex for whole numbers or numbers with quarter decimal

Clash Royale CLAN TAG#URR8PPP
Regex for whole numbers or numbers with quarter decimal
I need a regex that only allows for whole numbers or numbers with a quarter decimal.
So far I have this, however this code /[^.]+.25|[^.]+.50|[^.]+.75|[^.]+.00/ forces user to type a number with a decimal. I'm looking for something more flexible.
/[^.]+.25|[^.]+.50|[^.]+.75|[^.]+.00/
Valid
0
0.
.25
.5
.75
3
1.
1.00
5.0
4.25
8.50
8.75
Invalid
1.2
.3
.
empty space
^(?!.?$)d*.?(?:[27]5|5?0?)?$
@revo Doesn't match
1.00.– melpomene
Aug 10 at 19:02
1.00
@melpomene I don't see it in samples or there is no explicit rule for it and I'm not supposed to read the regex from OP and find the rules. But if that's the case, it's just a matter of adding a character class
^(?!.?$)d*.?(?:[27]5|[50]0?)?$.– revo
Aug 10 at 19:22
^(?!.?$)d*.?(?:[27]5|[50]0?)?$
melpomene you are right, I did not account for 1.00 which would be valid. Thanks for your efforts revo and melpomene.
– Terry H
Aug 12 at 10:35
2 Answers
2
You might use an alternation to match either an optional digit followed by a dot the quarter decimal part or match one or more digits followed by an optional dot.
^(?:d*.(?:[27]5|50?|00?)|d+.?)$
^(?:d*.(?:[27]5|50?|00?)|d+.?)$
Explanation
(?:
d*.
(?:[27]5|50?|00?)
|
d+.?
)
$
Here's one way:
/A (?= .? [0-9] ) [0-9]* (?: . (?: [05]0? | [27]5 )? )? z/x
Or with comments:
/
A # beginning of string
(?= # look-ahead
.? # a dot (optional)
[0-9] # a digit
)
# ^ this part ensures that there is at least one digit in the string.
# in the following regex all parts are optional.
[0-9]* # the integer part: 0 or more digits
(?: # a group: the decimal part
. # a dot
(?: # another group for digits after the decimal point
[05]0? # match 0 or 5, optionally followed by 0 (0, 00, 5, 50)
|
[27]5 # ... or 2 or 7, followed by 5 (25, 75)
)? # this part is optional
)? # ... actually, the whole decimal part is optional
z # end of string
/x
It's a bit tricky because all parts of the number are optional in some way:
.25
0
0.
The main regex is written in a way that makes all parts optional, but there's a look-ahead assertion before it to make sure that the whole string is not empty or just ..
.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try
^(?!.?$)d*.?(?:[27]5|5?0?)?$. See live demo here regex101.com/r/Nw5Amy/1– revo
Aug 10 at 18:59