为什么两个打印的数字不同?
#!/usr/bin/env perl use warnings; use 5.10.1; my $sep = ''; my $number = 110110110110111; $number =~ s/(\d)(?=(?:\d{3})+\b)/$1$sep/g; say "A: <$number>"; $number =~ s/\Q$sep\E//g; say "B: <$number>";
输出:
A: <110110110110111> B: <11111111111>
解决方法
报价
man perlop:
If the pattern evaluates to the empty string,the last successfully
executed regular expression is used instead.
尝试在第二次替换之前插入一个成功的正则表达式匹配,看看发生了什么:
(my $foo = '1') =~ s/1/x/; # successfully match “1” $number =~ s///g; # now you’re deleting all 1s say "B: <$number>"; # <0000>
我会说这应该被弃用,并且通过使用警告来警告,很难看到好处.