1
I’m having trouble making the following mask.
Valid values:
1.98.212
1.98
1
What I’ve managed to do so far:
\d{0,1}(\.\d{0,2})?(\.\d{0,3})?
You’re wrong because you can insert 1,999, because (.\d{0,2})?
is optional.
1
I’m having trouble making the following mask.
Valid values:
1.98.212
1.98
1
What I’ve managed to do so far:
\d{0,1}(\.\d{0,2})?(\.\d{0,3})?
You’re wrong because you can insert 1,999, because (.\d{0,2})?
is optional.
1
First, {0,1}
means "at least zero and at most 1", which means that the first digit is also optional. But since it seems to be mandatory, then remove it from there.
The second part after the point: either it has 2 digits, or it has 2 digits and 3 digits, so it should actually look like this:
^\d(\.\d{2}(\.\d{3})?)?$
That is, after the 2 digits, it can optionally have 3 digits, but all this stretch (2 digits plus 3 optional digits) is also optional.
I switched out the quantifiers by the exact amount. {0,2}
means "at least zero and at most 2", that is, if you have zero, one or two digits, it works. Already {2}
means "exactly 2", which is what you need.
The point corresponds to any character, then to accept only the dot character, we must escape it with \
.
I also used the markers ^
and $
which indicate respectively the beginning and end of the string, to ensure that it has no more characters.
See here the regex working.
Browser other questions tagged delphi regex
You are not signed in. Login or sign up in order to post.
got it.. so I have to leave a grouping optional and go nesting the others.. valeu!
– Luciano Victor