Reputation: 2358
I am busy working on this and thought I would put it our there.
It must be a number with a maximum of 3 units and maximum of 5 decimal places etc
Valid
Invalid
EDIT It needs to be greater than or equal to zero.
Upvotes: 6
Views: 19351
Reputation: 568
Built off of answer by @ohaal above for currency $0.00 - $999.99
(\$(?!\d{1,3},\d{1,3})(?!\d{4})\d{1,3}(?!\d{1,3}\.\d\D)(?:\.\d{2,5})?)(?!\.)(?!,\d{3})
Upvotes: 0
Reputation: 5268
In light of your recent changes to the question, here is an updated regex which will match all >= 0 and <1000
^\d{1,3}(?:\.\d{1,5})?$
^\___/ \/ ^\/\___/ |
| ^ ^ | | | `- Previous is optional (group of dot and minimum 1 number between 0-9 and optionally 4 extra numbers between 0-9)
| | | | | `- Match 1-5 instances of previous (single number 0-9)
| | | | `- Match a single number 0-9
| | | `- The dot between the 1-3 first number(s) and the 1-5 last number(s).
| | `- This round bracket should not create a backreference
| `- Match 1-3 instances of previous (single number 0-9)
`- Match a single number 0-9
^
is start of line, $
is end of line.
Valid
Invalid
Upvotes: 14
Reputation: 1379
Check out http://www.regular-expressions.info/numericranges.html there's a lot of examples about numbers and discussions about pro- and cons of different approaches.
Upvotes: 0
Reputation: 35267
/\d{1,3}(\.\d{1,5})?\b/ # the boundary \b prevents matching numbers after the max of 5
Edit: A quick search shows that as usual, there's a CPAN module for this.
@ohaal correctly points out that this will also match 0, which is invalid. I suggest a combination of this regex and a test that the matched value is greater than 0.
Also see http://www.perlmonks.org/?node_id=614452
Upvotes: 1
Reputation: 2113
^([0-9].[0-9]{0,5}|[1-9][0-9]{0,2}.{0,1}[0-9]{0,5})$
Accepted 999.99999 99.9 9 0.99999
Rejected -0.1 999.123456 AAA AAA.99999 0
Upvotes: 1