gpt4 book ai didi

regex - 是否有正则表达式来匹配最多 7 位数字的 float ?

转载 作者:行者123 更新时间:2023-12-02 07:37:21 25 4
gpt4 key购买 nike

我正在寻找一个正则表达式来匹配最多 7 位数字的 float 。我不知道如何处理这个小数点。甚至可以将其与正则表达式匹配吗?小数点左边至少有 1 位,右边有 0-6 位,但总位数不得超过 7 位。

例子:

好:

  • 1.234567
  • 0.1
  • 1234567
  • 1

差:

  • .1234567
  • 12345678
  • 1.2.34567

最佳答案

以下应该有效:

^(?!.*\..*\.|\d{8})\d[\d.]{,7}$

示例: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

关于regex - 是否有正则表达式来匹配最多 7 位数字的 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14963970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com