我需要一个正则表达式来匹配两个,整数值以及浮点数(而浮点数有一个“。”作为分隔符)。这些数字总是在括号内,可能有一个前导“+”。
什么应该有效:
什么应该是无效的:
答案 0 :(得分:22)
这应该适用于大多数perl,如正则表达式引擎:
/(\d+(\.\d+)?)/
答案 1 :(得分:14)
不幸的是,目前接受的答案是有缺陷的(它将匹配" 1a5",但它不会匹配" .5")。所以我决定编写自己的Regex,以及一个廉价的Perl脚本来测试它。
正则表达式将匹配一个整数或浮点数,前面带有+/-符号。我不在乎E符号,所以我忽略了它
nhahtdh提到"(5。)和(.6)":" 5。"对我来说看起来不对,尾随时期没有任何意义。然而," .6"确实是有效的,因为领先时期表示正值< 1(即" 0.6"它的缩写)
最初的(?=.)
是为了确保空白字符串不匹配
作为奖励,我使用嵌套组,以便您可以轻松提取数字的不同部分:
^(?=.)([+-]?([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
请告诉我是否有我忽略的事情。
执行大括号是微不足道的,只需将整个内容包装在ESCAPED括号中:
^\((?=.)([+-]?([0-9]*)(\.([0-9]+))?)\)$
答案 2 :(得分:-1)
我不确定,但是我认为接受的答案不会接受.6
。
my $float = qr/(\([-+]?\d*\.?\d+\))/;
my $test = "this is my +15.8 (-17.3) 0.9 (8) .7 -.6 string"
. "(+5.8) -6.3 (0.9) 8 (.9) (-.16)";
while ($test =~ /$float/g) {
printf("<$1>\n");
}
我从某个地方偷走了这种模式。也许https://www.regular-expressions.info/floatingpoint.html。
<(-17.3)>
<(8)>
<(+5.8)>
<(0.9)>
<(.9)>
<(-.16)>
更新:添加了括号。
答案 3 :(得分:-4)
var str = "8,50 € *";
console.log(str.replace(/[&\/\\#+()$~%.'":*€?<>{}]/g, ''));