Regular expression for range> = 0 but less than 1000

I was busy with it over it and thought I would put it there.

This number should contain no more than three units and no more than 5 decimal places, etc.

Really

  • 999,99999
  • 99.9
  • 9
  • 0,99999
  • 0

Invalid

  • -0.1
  • 999.123456
  • AAA
  • AAA.99999

EDIT It must be greater than or equal to zero.

+4
source share
4 answers

In light of your recent changes in the question, here is an updated regular expression that 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 

^ - beginning of line, $ - end of line.

Really

  • 999,99999
  • 999.0
  • 999
  • 99.9
  • 99.0
  • 99
  • 9
  • 0.1
  • 0.01
  • 0.001
  • 0.0001
  • 0,99999
  • 0.01234
  • 0.00123
  • 0.00012
  • 0.00001
  • 0.0
  • 0.00000
  • 0
  • 000,00,000
  • 000

Invalid

  • -0.1
  • 999.123456
  • AAA
  • AAA.99999
  • 0.
  • 0.123
+12
source
 /\d{1,3}(\.\d{1,5})?\b/ # the boundary \b prevents matching numbers after the max of 5 

Edit: A quick search reveals that, as usual, there is a CPAN module for this.

Regexp :: common :: number

@ohaal correctly points out that this will also match 0, which is incorrect. I suggest a combination of this regex and test that the matching value is greater than 0.

Also see http://www.perlmonks.org/?node_id=614452

+1
source
  ^([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

+1
source

Check out http://www.regular-expressions.info/numericranges.html there are a lot of examples about numbers and discussions about the pros and cons of different approaches.

0
source

Source: https://habr.com/ru/post/1397997/


All Articles