Let me break it down into four regular expressions. At least one of the four must match.
# decimal, 1-2 digits \.\d{1,2}
which can then be combined into one regular expression like this.
(\.\d{1,2}|\d{1,4}\.?\d{0,2}|\d{5}\.?\d?|\d{6}\.?)
Then add the carriage ( ^ ) and insert ( $ ) at the beginning and end of the line.
^(\.\d{1,2}|\d{1,4}\.?\d{0,2}|\d{5}\.?\d?|\d{6}\.?)$
It doesn't scale very well (for example, if you want to match 100 digits with an accuracy of 20 after the decimal point), but it works, and it's relatively easy to understand.
If you don't need regular expression, there are simpler ways to solve this problem. :)
Patrick McElhaney
source share