-1

试图编写一个正则表达式函数,以不允许任何不是 (19,6) 小数的内容保存到我的数据库中

所以 9.999 可以在 68.1234557 失败的地方工作。还需要 1234567890123456789.1 才能失败。从 19 个地方感知它。

也支持负数

4

3 回答 3

1

像这样的东西会起作用:

^[+-]?\d{,13}(\.\d{,6})?$

这将匹配:

  • 字符串的开头 ( ^)
  • 可选的加号或减号(文字+-
  • 0 到 13 位 (19 - 6)
  • 可选组:
    • 一个字面.
    • 0 到 6 位数字
  • 字符串的结尾 ( $)

开始/结束锚点用于禁止匹配的子字符串之前或之后的其他字符。

于 2013-09-23T14:20:59.750 回答
1
^\d{,19}(\.\d{,6})?$

如果您需要小数点:

^\d{,19}\.\d{1,6}$
于 2013-09-23T14:21:28.760 回答
1

怎么样 :^(?=^.{1,19}$)\d+(?:\.\d{1,6})?$

解释:

The regular expression:

(?-imsx:^(?=^.{1,19}$)\d+(?:\.\d{1,6})?$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    ^                        the beginning of the string
----------------------------------------------------------------------
    .{1,19}                  any character except \n (between 1 and
                             19 times (matching the most amount
                             possible))
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \.                       '.'
----------------------------------------------------------------------
    \d{1,6}                  digits (0-9) (between 1 and 6 times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

一个 perl 脚本来测试它:

use strict;
use warnings;
use 5.010;

my $re = qr~^(?=^.{1,19}$)\d+(?:\.\d{1,6})?$~;
while(<DATA>) {
    chomp;
    say /$re/ ? "OK : $_" : "KO : $_";
}
__DATA__
9.999
123456.123456
68.1234557
1234567890123456789.1

输出:

OK : 9.999
OK : 123456.123456
KO : 68.1234557
KO : 1234567890123456789.1
于 2013-09-23T14:29:14.957 回答