I got a regex that fails on validating my input.
My regex: \d{1,5}([\.,]\d{0,2})?
It should validate square meters (decimal values) with a separator of either . or , and should allow up to five digits (and at least one) before the separator and up to two after it.
So it should accept:
1
1,0
1.0
12345
12345,10
12345.10
But not
.1
123456
12345,123
In the group you’re capturing,
([\.,]\d{0,2})?, you allow the numbers to appear between0and2times, however, the entire group is marked as optional per the ending?. Because of this, the range should be set to{1,2}instead:If your input is specifically a number (and not a full sentence), I would also recommend adding a leading
^and an ending$. This will force the regex to check from the start of the string to the end (respectively). The final regex should be:If you’re using this regex for more than validation and also want to view the matched values, you should change the current group that’s matching (the decimal portion) to a non-matching group (change
(to(?:) and then add parentheses around the full regex: