不幸的是,当前接受的答案是有缺陷的(它将匹配“1a5”,但不会匹配“.5”)。所以我决定编写自己的 Regex,以及一个便宜的 Perl 脚本来测试它。
要求
正则表达式将匹配一个整数或浮点数,前面有一个 +/- 符号。我不关心 E 符号,所以我忽略了它。
nhahtdh 提到“(5.)和(.6)”:“5。” 在我看来不正确,尾随句号没有目的。但是,“.6”确实有效,因为前导句点表示正值 < 1(即,它是“0.6”的缩写)。
最初(?=.)
是为了确保空白字符串不匹配。
作为奖励,我使用了嵌套组,以便您可以轻松提取数字的不同部分:
- $1 = 整数
- $2 = 整数部分
- $3 = 带前导周期的小数部分
- $4 = 小数部分
正则表达式
^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$
测试
$ perl -e 'use strict; use warnings; my $a = "1.0"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
MATCH
Float:<1.0>
Integer:<1>
Fraction:<0>
$ perl -e 'use strict; use warnings; my $a = "1"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
MATCH
Float:<1>
Integer:<1>
No fraction
$ perl -e 'use strict; use warnings; my $a = "9.9"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
MATCH
Float:<9.9>
Integer:<9>
Fraction:<9>
$ perl -e 'use strict; use warnings; my $a = "10000000"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
MATCH
Float:<10000000>
Integer:<10000000>
No fraction
$ perl -e 'use strict; use warnings; my $a = "+15"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
MATCH
Float:<+15>
Integer:<15>
No fraction
$ perl -e 'use strict; use warnings; my $a = "1,5"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
NO MATCH
$ perl -e 'use strict; use warnings; my $a = "a"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
NO MATCH
$ perl -e 'use strict; use warnings; my $a = "1 5"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
NO MATCH
$ perl -e 'use strict; use warnings; my $a = "1+5"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
NO MATCH
$ perl -e 'use strict; use warnings; my $a = "5."; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
NO MATCH
$ perl -e 'use strict; use warnings; my $a = ".6"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
MATCH
Float:<.6>
Integer:<>
Fraction:<6>
$ perl -e 'use strict; use warnings; my $a = ".5"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
MATCH
Float:<.5>
Integer:<>
Fraction:<5>
$ perl -e 'use strict; use warnings; my $a = "1a5"; if ($a =~ /^(?=.)([+-]?([0-9]*)(\.([0-9]+))?)$/) { print "MATCH\nFloat:<$1>\n"; print defined $2 ? "Integer:<$2>\n" : "No integer\n"; print defined $4 ? "Fraction:<$4>\n" : "No fraction\n"; } else { print "NO MATCH\n"; }'
NO MATCH
如果有什么我忽略了,请告诉我。
添加
强制大括号是微不足道的,只需将整个东西包裹在 ESCAPE 括号中:
^\((?=.)([+-]?([0-9]*)(\.([0-9]+))?)\)$