试图编写一个正则表达式函数,以不允许任何不是 (19,6) 小数的内容保存到我的数据库中
所以 9.999 可以在 68.1234557 失败的地方工作。还需要 1234567890123456789.1 才能失败。从 19 个地方感知它。
也支持负数
试图编写一个正则表达式函数,以不允许任何不是 (19,6) 小数的内容保存到我的数据库中
所以 9.999 可以在 68.1234557 失败的地方工作。还需要 1234567890123456789.1 才能失败。从 19 个地方感知它。
也支持负数
像这样的东西会起作用:
^[+-]?\d{,13}(\.\d{,6})?$
这将匹配:
^
)+
或-
).
$
)开始/结束锚点用于禁止匹配的子字符串之前或之后的其他字符。
^\d{,19}(\.\d{,6})?$
如果您需要小数点:
^\d{,19}\.\d{1,6}$
怎么样 :^(?=^.{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