如何从特定的数字中获取一个值?
假设number为20040819。我想用Perl得到最后两位数,即19。
发布于 2010-10-01 19:19:49
my $x = 20040819;
print $x % 100, "\n";
print substr($x, -2);发布于 2010-10-01 19:23:02
Benoit的答案恰到好处,我也会用到,但为了像你在标题中建议的那样使用模式搜索,你可以这样做:
my $x = 20040819;
if ($x =~ /\d*(\d{2})/)
{
$lastTwo = $1;
}发布于 2010-10-02 00:00:32
substr("20040819", -2); 也可以使用 - Date和time正则表达式,如
use strict;
use Regexp::Common qw(time);
my $str = '20040819' ;
if ($str =~ $RE{time}{YMD}{-keep})
{
my $day = $4; # output 19
#$1 the entire match
#$2 the year
#$3 the month
#$4 the day
}https://stackoverflow.com/questions/3838660
复制相似问题