我正在寻找一个正则表达式来匹配最多7位数的浮点数.我无法弄清楚如何处理小数点.甚至可以将它与正则表达式相匹配吗?小数点左边必须至少有1位数字,右边有0-6位数字,但总位数必须是7或更少.
例子:
好:
坏:
以下应该有效:
^(?!.*\..*\.|\d{8})\d[\d.]{,7}$
Run Code Online (Sandbox Code Playgroud)
示例:http://www.rubular.com/r/gglVngm0pH
说明:
^ # 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93 次 |
| 最近记录: |