Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Perl使用正则表达式从文本字符串中提取浮点数_Regex_String_Perl - Fatal编程技术网

Regex Perl使用正则表达式从文本字符串中提取浮点数

Regex Perl使用正则表达式从文本字符串中提取浮点数,regex,string,perl,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 我想提取一个不太精确的值,比如说2个小数点 谢谢。您可以使

虽然看起来很简单,但我并不是一个使用正则表达式的perl代码的好例子,正则表达式从(任何)字符串中提取浮点数,如下所示:

my $str = "process.pl: process workflow took 2.41153311729431 seconds.";
my $processTime = parseFloatFromString($str);
print "$processTime\n";

and gives 2.41
我想提取一个不太精确的值,比如说2个小数点


谢谢。

您可以使用正则表达式提取数字,然后按如下方式打印:

my ( $number ) = ( $str =~ /(\d+(?:\.\d+)?)/ );
printf "%.2f", $number;
为了清楚起见,上面的代码被简化了。您还应处理图案不匹配(无数字)的情况,例如:

根据需要调整正则表达式,除非要处理浮点数的所有可能表示形式


如果您真的想处理所有可能的输入,那么请按照Dave Cross的建议使用库。但由于“要求我们推荐或查找书籍、工具、软件库、教程或其他非现场资源的问题与堆栈溢出无关……”,我不会建议任何库。此外,看起来您只是在学习Perl,并且愿意学习如何使用Perl正则表达式实际完成工作。所以我建议你继续阅读,然后考虑你是否真的需要一个库。

< P>有两个步骤:

  • 从字符串中提取浮点数
  • 将这些数字转换为所需的精度
  • 第1步比你想象的要难,所以我建议使用现成的正则表达式(就像我在这里使用的一样)

    然后可以使用
    sprintf()
    printf()
    更改精度

    printf "%0.2f\n" for @floats;
    

    简单的数字解析就是这样的
    (?:\d+(?:\。\d*)?\。\d+

    若要将其修改为接受0-2位小数,则

    (?:\d+(?:\。\d{0,2})\。\d{1,2})

    扩大

     (?:
          \d+ 
          (?: \. \d{0,2} )?
       |  \. \d{1,2} 
     )
    
    注:此项没有边界规范。
    因此,必须对其进行修改,以便在全球范围内使用

    通常,您只需在正则表达式之后添加一个
    \d*
    ,并包含一个捕获
    组。
    这将捕获您需要的内容,并匹配您不需要的内容
    继续进行下一个号码的匹配位置

    (\d+(?:\。\d{0,2})\。\d{1,2})\d*

    用法

    如果只想更改预先存在的浮点字符串,
    您可以将正则表达式更改为需要小数点。
    与顶部的常规相同,删除该选项
    在它的群集组上,不会仅匹配数字

    (\d+(?:\。\d*)\124;\。\ d+

    如果使用此选项,您可以使用替代表单中的sprint()。
    这将一次完成所有操作,而无需提取、重新组装或其他方式
    丢弃现有字符串

    你有很多选择,这只是其中之一

    Perl

     use strict;
     use warnings;
    
     my $str = "process.pl: process 3 workflow took .0 days, 2.41153311729431 secs, 2411.53311729431 ms, 2411533.11729431 us.";
    
     # To print without modify the string
     print $str =~ s/(\d+(?:\.\d*)|\.\d+)/sprintf("%0.2f",$1)/erg, "\n";
    
     # Or, print and modify the string at the same time
     print $str = $str =~ s/(\d+(?:\.\d*)|\.\d+)/sprintf("%0.2f",$1)/erg, "\n";
    
    输出

     process.pl: process 3 workflow took 0.00 days, 2.41 secs, 2411.53 ms, 2411533.12 us.
     process.pl: process 3 workflow took 0.00 days, 2.41 secs, 2411.53 ms, 2411533.12 us.
    
  • 如布罗丁所示,a/提取字段,然后b/正确呈现。如果输出是一致的,我就拆分 它:
  • 解决问题的根源!将原始输出更改为生成2进制 指向你不需要做额外的工作来正确地提取和呈现它

  • 没有必要在
    (?:\.\d{0,2})?\.\d{1,2})中“匹配你不需要的东西”
    第二个备选方案永远不会匹配,因为它总是与第一个方案匹配。@Borodin-
    没有必要“匹配你不需要的东西”
    你能澄清一下吗
    (?:\。\d{0,2})\。\d{1,2})
    你从哪里得到这个正则表达式的???@Borodin-让我帮你
    (?:\。\d{0,2})\\d{1,2}=)
    (拆分“”,$str)[4]
    @SinanÜnür:更好的是
    (拆分“”,$str,6)[4]
    。我最后一次听到,perl优化了
    my(undef,undef,undef,$f4)=split“”,$str
    ,但等效的列表片没有改进。@kybof:是的,不是我。@kybof:
    (split“”,$str)[4]
    正确<代码>(拆分/,$str)[4]
    不是。
     use strict;
     use warnings;
    
     my $str = "process.pl: process 3 workflow took .0 days, 2.41153311729431 secs, 2411.53311729431 ms, 2411533.11729431 us.";
    
     # To print without modify the string
     print $str =~ s/(\d+(?:\.\d*)|\.\d+)/sprintf("%0.2f",$1)/erg, "\n";
    
     # Or, print and modify the string at the same time
     print $str = $str =~ s/(\d+(?:\.\d*)|\.\d+)/sprintf("%0.2f",$1)/erg, "\n";
    
     process.pl: process 3 workflow took 0.00 days, 2.41 secs, 2411.53 ms, 2411533.12 us.
     process.pl: process 3 workflow took 0.00 days, 2.41 secs, 2411.53 ms, 2411533.12 us.
    
    $procTime=split(' ',$str)[4]; # extract ... Thx to Sinan for improving this
    printf "process time: %7.2f\n", $procTime; # present