Regular Expression Numbers between .xx - xxx.xx

I want to catch any number greater than 0, from 0.01 is also 999, where .01 is also acceptable.

^([1-9][0-9]*(\.[0-9]*[1-9])?|0\.[0-9]*[1-9]) 

Must match: (if a decimal number exists, a maximum of 3 numbers before and after 2 after.)

 .01 .1 0.01 0.1 1 10 10.1 10.11 105.1 999.99 88.00 

Failed:

 12345678.54 00564.5412 00.451 1. ,25 ..25 0025 01 00,25 0 .0 0.001 999.001 e123.12 1000 

http://regex101.com/r/qZ5lC6/1 -.x and character restriction before and after an optional decimal number is where it gets unpleasant.

+1
regex
source share
2 answers

based on what you posted use this template

 ^(?=.*[1-9])(?!0\d)(\d{0,3}(?:\.\d{1,2})?)$ 

Demo

 ^ Start of string/line (?= Look-Ahead . Any character except line break * (zero or more)(greedy) [1-9] Character Class [1-9] ) End of Look-Ahead (?! Negative Look-Ahead 0 "0" \d <digit 0-9> ) End of Negative Look-Ahead ( Capturing Group \1 \d <digit 0-9> {0,3} (repeated {0,3} times) (?: Non Capturing Group \. literal "." \d <digit 0-9> {1,2} (repeated {1,2} times) ) End of Non Capturing Group ? (zero or one)(greedy) ) End of Capturing Group \1 $ End of string/line 
+8
source share

For completeness, here is a solution without workaround statements:

 ^([1-9]\d{0,2}(\.\d{1,2})?|0?\.(\d[1-9]|[1-9]\d?))$ 

(If necessary, capture the capture group)

[1-9]\d{0,2}(\.\d{1,2})? corresponds to integers from 1 to 999 or decimal numbers from 1.00 to 999.99, both cases without leading 0, and the fractional part can have 1 or 2 digits.

0?\.(\d[1-9]|[1-9]\d?) Corresponds to 0.01-0.99, while 0 in the integer part is optional, and the fractional part consists of 1 or 2 digits.

+3
source share

All Articles