How about this:
^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$
Explanation:
^
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
Tim Pietzcker Dec 22 '11 at 21:25 2011-12-22 21:25
source share