Reputation: 4044
It seems I'm stuck again with a simple regex.
What I'd like:
Allowed:
100
999,0
999,999
999,99Disallowed:
-1
0
999,
999,9999
This is what I have so far:
^[0-9]{1,3}(?:\,[0-9]{1,3})?$
Any tips?
Upvotes: 8
Views: 12632
Reputation: 642
This regex should work :
^[1-9]\d{0,2}(?:,\d{1,3})?$
Here is the explanation :
^[1-9]
: It should begin with a number between 1 and 9
\d{0,2}
: Followed by minimum 0, maximum 2 digits (0-9)
(?:,
: Followed by a comma
\d{1,3})?
: IF there is a comma it should be followed by one to three digits
$
: End of line
Thanks @dev-null for this link: Explanation
Upvotes: 7
Reputation: 784998
You can use this regex:
/^[1-9]\d{0,2}(?:\,\d{1,3})?$/
Main difference from OP's regex is use of [1-9]
which matches digits 1 to 9 before rest of the regex.
Upvotes: 8