Reputation: 1668
I am looking for a regex to match a float with at most 7 digits. I cant figure out how to handle the decimal point in this. Is it even possible to match this with a regex? There has to be atleast 1 digit to the left of the decimal, and 0-6 digits to the right, but the total number of digits has to be 7 or less.
examples:
Good:
Bad:
Upvotes: 2
Views: 122
Reputation: 208475
The following should work:
^(?!.*\..*\.|\d{8})\d[\d.]{,7}$
Example: http://www.rubular.com/r/gglVngm0pH
Explanation:
^ # beginning of string anchor
(?! # start negative lookahead (fail if following regex can match)
.*\..*\. # two or more '.' characters exist in the string
| # OR
\d{8} # eight consecutive digits in the string
) # end negative lookahead
\d # match a digit
[\d.]{,7} # match between 0 and 7 characters that are either '.' or a digit
$ # end of string anchor
Upvotes: 7