$string1 = "2_0_1AU13682.0AV+0.2"
$string2 = "2_0_1AT+0.1CD13681.9"
我有这两个字符串。如何从两者中提取7个字符十进制数?
来自字符串1的13682.0
和字符串2 13681.9
。
十进制数始终为7个字符,始终采用xxxxx.x
答案 0 :(得分:5)
尝试使用 regex 和匹配运算符执行此操作:
my $string1 = "2_0_1AU13682.0AV+0.2";
my ($res) = $string1 =~ m/(\d{5}\.\d)/;
print $res, "\n";
答案 1 :(得分:2)
使用正则表达式,如下所示:
my @data = qw( 2_0_1AU13682.0AV+0.2 2_0_1AT+0.1CD13681.9 );
foreach my $str (@data) {
if ($str =~ /(\d{5}\.\d)/) {
print $1, "\n";
}
}
输出:
13682.0
13681.9