I’m trying to replicate a function which uses this pattern matching in the codes
^1000([.][0]{1,3})?$|^\d{1,3}$|^\d{1,3}([.]\d{1,3})$|^([.]\d{1,3})$
This regex allows number from 0 to 1000, but so is
\b([0-9]{1,3}|1000)\b
However with the update I needed to allow negative values from -1000 to 1000 and also 2 decimal values in the range and I’m completely lost with what I need to figure out with the original pattern.
I’d appreciate any explanation on the pattern used in the code since there are more ranges that I have to update.
There are a few difference between the two regexes. Firtly, the original one requires the digit to be everything in the string. The second one will find any separate number in the string. Example:
The first regex will not match at all, although the number is of the specified format, because the
$anchor cannot match. The second regex will match, but only1000.Now depending on whether you want to match only full strings or substrings, too, this is probably what you are looking for:
or
You can test them in this working demo.
The main caveat, that is also displayed in the demo is that leading zeros are permitted.