Regex greater than zero with 2 decimal places

I need RegEx for a numeric value with an accuracy of two decimal places greater than zero and may or may not have zero in one column. I also have to add ... integers in order. See below, but there may be spaces with spaces forward or backward.

Good values: .1 0.1 1.12 123.12 92 092 092.13 Error values: 0 0.0 0.00 00 1.234 -1 -1.2 Anything less than zero 
+20
decimal regex
Dec 22 '11 at 21:05
source share
5 answers

How about this:

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

Explanation:

 ^ # Start of string \s* # Optional whitespace (?=.*[1-9]) # Assert that at least one digit > 0 is present in the string \d* # integer part (optional) (?: # decimal part: \. # dot \d{1,2} # plus one or two decimal digits )? # (optional) \s* # Optional whitespace $ # End of string 

Test in Python:

 >>> import re >>> test = [".1", "0.1", "1.12", "123.12", "92", "092", "092.13", "0", "0.0", "0.00", "00", "1.234", "-1", "-1.2"] >>> r = re.compile(r"^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$") >>> for item in test: ... print(item, "matches" if r.match(item) else "doesn't match") ... .1 matches 0.1 matches 1.12 matches 123.12 matches 92 matches 092 matches 092.13 matches 0 doesn't match 0.0 doesn't match 0.00 doesn't match 00 doesn't match 1.234 doesn't match -1 doesn't match -1.2 doesn't match 
+52
Dec 22 '11 at 21:25
source share
β€” -

[0-9]+\.[0-9]{1,2}

This will find:

  • At least one number
  • Decimal point
  • One or two digits after the decimal point.
+1
Dec 22 2018-11-21T00:
source share

/^[0-9]*(\.{1})?([0-91-9][1-9])?$/

try it, it will go through all your affairs

+1
Nov 22 '14 at 19:31
source share

The code below allows both , and . ,

 ^(?=.*[1-9])[0-9]*[.,]?[0-9]{1,2}$ 
+1
Jul 19 '19 at 14:00
source share

This expression does not allow any white spaces at the beginning and end

 /^\d*(?:\.\d{1,2})*$/ 
-3
Oct 20 '17 at 8:04 on
source share



All Articles