SC-*_*-SL -1 regex string perl
虽然看起来很简单 - 我没有使用正则表达式的perl代码的一个很好的例子,它提取浮出一个(任何)字符串,如下所示:
my $str = "process.pl: process workflow took 2.41153311729431 seconds.";
my $processTime = parseFloatFromString($str);
print "$processTime\n";
and gives 2.41
Run Code Online (Sandbox Code Playgroud)
我想提取一个不太精确的值 - 例如2个小数点.
谢谢.
这有两个步骤:
第1步比你想象的更难,所以我建议使用现成的正则表达式(就像我在这里使用的那个Regexp::Common).
use Regexp::Common;
my @floats = $string =~ /($RE{num}{real})/g;
Run Code Online (Sandbox Code Playgroud)
然后您可以使用sprintf()或printf()更改精度.
printf "%0.2f\n" for @floats;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
940 次 |
| 最近记录: |