使用Perl匹配并提取.001以下的所有数字

时间:2015-03-27 08:59:07

标签: regex perl

这就是我的数据在文件中的排列方式

 c65194_g1_i1   -21.9509530657044   13.2266114245427    1.26719984587427e-44    1.73042474953361e-39
c29053_g1_i1    -21.7830220218917   13.0586642598416    4.05844989803944e-44    2.77100812913388e-39
c63097_g1_i1    -21.4616315169133   12.7372371455793    3.76550367403328e-43    1.71399451402538e-38
c15833_g1_i1    -21.3337075582514   12.6092961895881    9.1390133914486e-43 3.11994493417316e-38
c54104_g1_i1    -20.9476922994263   12.2232194785508    1.32699572263938e-41    3.62415801810041e-37
c72466_g1_i1    -20.7117075903545   11.9871882514396    6.8114568592363e-41 1.55023081902169e-36
c54551_g1_i1    -20.6402597636308   11.9157247731746    1.11763325249107e-40    2.18026298277025e-36
c16812_g1_i1    -19.9803901665057   11.2556673199183    1.0829030885624e-38 1.84844789073297e-34
c52174_g1_i1    -17.3946586876797   11.1994819594618    1.59840414284341e-38    2.42522308584424e-34
c61394_g3_i1    -19.7919324674417   11.0671382066934    3.99837801243703e-38    5.45998509488338e-34
c63720_g1_i1    -19.7442542319924   11.0194403765143    5.56416938462837e-38    6.9074104574357e-34
c52697_g1_i3    -19.7121643690573   10.9873369560405    6.95007628588691e-38    7.42715989045943e-34
c54027_g2_i2    -19.7096756788425   10.9848472017346    7.07063663549284e-38    7.42715989045943e-34
c55424_g1_i2    -14.9238636253627   10.5276723871648    1.67866916859075e-36    1.63736191654935e-32
c53640_g1_i1    -18.9525614294271   10.2273069100634    1.34402113000358e-35    1.22355203605092e-31
c49936_g1_i1    -18.9066537149149   10.1813654615637    1.84747309451501e-35    1.57676055263436e-31
c61346_g1_i2    -14.4532346909692   10.0567336871647    4.37960869176717e-35    3.51798508767215e-31
c52697_g1_i4    -18.637842793771    9.91233394513217    1.19009210093315e-34    9.02850149127368e-31
c53336_g2_i1    -18.5076080431514   9.78197660837233    2.93451761200646e-34    2.10906869740812e-30
c13587_g1_i1    -18.4719269225614   9.74625992614394    3.75786641185474e-34    2.56577723935412e-30
c48254_g1_i1    -18.4479382811535   9.72224687735655    4.43827627976478e-34    2.88604198753943e-30
c53513_g1_i1    -18.3774595626302   9.65169405756151    7.23348927630069e-34    4.48985967329655e-30
c52353_g2_i1    -18.3691781540535   9.64340370193521    7.6593042463829e-34 4.54746213636877e-30
c56356_g1_i3    -18.2534296819214   9.5275246522986         1.70868862762047e-33    9.72208231436306e-30
c41529_g2_i1    -18.1994639286288   9.47349434618108    2.48348881197431e-33    1.35653125887661e-29

是否可以使用Perl将这些数字提取到.001以下?所有以e - (随机值)结尾的否定和数字都在.001之下。我还没有找到解决问题的解决方案。请指教。

3 个答案:

答案 0 :(得分:5)

不需要正则表达式:

use Modern::Perl;

while(<DATA>) {
    chomp;
    my @nums = split;
    shift @nums;
    for (@nums) {
         say $_ if $_ < 0.001;
     }
}

__DATA__
 c65194_g1_i1   -21.9509530657044   13.2266114245427    1.26719984587427e-44    1.73042474953361e-39
c29053_g1_i1    -21.7830220218917   13.0586642598416    4.05844989803944e-44    2.77100812913388e-39
c63097_g1_i1    -21.4616315169133   12.7372371455793    3.76550367403328e-43    1.71399451402538e-38

<强>输出:

-21.9509530657044
1.26719984587427e-44
1.73042474953361e-39
-21.7830220218917
4.05844989803944e-44
2.77100812913388e-39
-21.4616315169133
3.76550367403328e-43
1.71399451402538e-38

答案 1 :(得分:1)

-\d+(?:\.\d+)?\b|\b\d+\.\d*e-\d+\b|\b\.000\d+\b

你可以试试这样的东西,虽然通过数学比较应该很容易。参见演示。

https://regex101.com/r/sJ9gM7/16

答案 2 :(得分:1)

这实际上会告诉您出现这些值的行和列:

#!/usr/bin/env perl

use strict;
use warnings;

while (my $obs = <DATA>) {
    next unless $obs =~ /\S/;
    my ($id, @vals) = split ' ', $obs;
    my @small = grep $vals[$_] < 0.001, 0 .. $#vals;
    for my $i (@small) {
        print "${id}[$i]: $vals[$i]\n";
    }
}

__DATA__
 c65194_g1_i1   -21.9509530657044   13.2266114245427    1.26719984587427e-44    1.73042474953361e-39
c29053_g1_i1    -21.7830220218917   13.0586642598416    4.05844989803944e-44    2.77100812913388e-39
c63097_g1_i1    -21.4616315169133   12.7372371455793    3.76550367403328e-43    1.71399451402538e-38
c15833_g1_i1    -21.3337075582514   12.6092961895881    9.1390133914486e-43 3.11994493417316e-38
c54104_g1_i1    -20.9476922994263   12.2232194785508    1.32699572263938e-41    3.62415801810041e-37
c72466_g1_i1    -20.7117075903545   11.9871882514396    6.8114568592363e-41 1.55023081902169e-36
c54551_g1_i1    -20.6402597636308   11.9157247731746    1.11763325249107e-40    2.18026298277025e-36
c16812_g1_i1    -19.9803901665057   11.2556673199183    1.0829030885624e-38 1.84844789073297e-34
c52174_g1_i1    -17.3946586876797   11.1994819594618    1.59840414284341e-38    2.42522308584424e-34
c61394_g3_i1    -19.7919324674417   11.0671382066934    3.99837801243703e-38    5.45998509488338e-34
c63720_g1_i1    -19.7442542319924   11.0194403765143    5.56416938462837e-38    6.9074104574357e-34
c52697_g1_i3    -19.7121643690573   10.9873369560405    6.95007628588691e-38    7.42715989045943e-34
c54027_g2_i2    -19.7096756788425   10.9848472017346    7.07063663549284e-38    7.42715989045943e-34
c55424_g1_i2    -14.9238636253627   10.5276723871648    1.67866916859075e-36    1.63736191654935e-32
c53640_g1_i1    -18.9525614294271   10.2273069100634    1.34402113000358e-35    1.22355203605092e-31
c49936_g1_i1    -18.9066537149149   10.1813654615637    1.84747309451501e-35    1.57676055263436e-31
c61346_g1_i2    -14.4532346909692   10.0567336871647    4.37960869176717e-35    3.51798508767215e-31
c52697_g1_i4    -18.637842793771    9.91233394513217    1.19009210093315e-34    9.02850149127368e-31
c53336_g2_i1    -18.5076080431514   9.78197660837233    2.93451761200646e-34    2.10906869740812e-30
c13587_g1_i1    -18.4719269225614   9.74625992614394    3.75786641185474e-34    2.56577723935412e-30
c48254_g1_i1    -18.4479382811535   9.72224687735655    4.43827627976478e-34    2.88604198753943e-30
c53513_g1_i1    -18.3774595626302   9.65169405756151    7.23348927630069e-34    4.48985967329655e-30
c52353_g2_i1    -18.3691781540535   9.64340370193521    7.6593042463829e-34 4.54746213636877e-30
c56356_g1_i3    -18.2534296819214   9.5275246522986         1.70868862762047e-33    9.72208231436306e-30
c41529_g2_i1    -18.1994639286288   9.47349434618108    2.48348881197431e-33    1.35653125887661e-29

输出:

c65194_g1_i1[0]: -21.9509530657044
c65194_g1_i1[2]: 1.26719984587427e-44
c65194_g1_i1[3]: 1.73042474953361e-39
c29053_g1_i1[0]: -21.7830220218917
c29053_g1_i1[2]: 4.05844989803944e-44
c29053_g1_i1[3]: 2.77100812913388e-39
c63097_g1_i1[0]: -21.4616315169133
c63097_g1_i1[2]: 3.76550367403328e-43
c63097_g1_i1[3]: 1.71399451402538e-38
c15833_g1_i1[0]: -21.3337075582514
c15833_g1_i1[2]: 9.1390133914486e-43
c15833_g1_i1[3]: 3.11994493417316e-38
c54104_g1_i1[0]: -20.9476922994263
c54104_g1_i1[2]: 1.32699572263938e-41
c54104_g1_i1[3]: 3.62415801810041e-37
c72466_g1_i1[0]: -20.7117075903545
c72466_g1_i1[2]: 6.8114568592363e-41
c72466_g1_i1[3]: 1.55023081902169e-36
c54551_g1_i1[0]: -20.6402597636308
c54551_g1_i1[2]: 1.11763325249107e-40
c54551_g1_i1[3]: 2.18026298277025e-36
c16812_g1_i1[0]: -19.9803901665057
c16812_g1_i1[2]: 1.0829030885624e-38
c16812_g1_i1[3]: 1.84844789073297e-34
c52174_g1_i1[0]: -17.3946586876797
c52174_g1_i1[2]: 1.59840414284341e-38
c52174_g1_i1[3]: 2.42522308584424e-34
c61394_g3_i1[0]: -19.7919324674417
c61394_g3_i1[2]: 3.99837801243703e-38
c61394_g3_i1[3]: 5.45998509488338e-34
c63720_g1_i1[0]: -19.7442542319924
c63720_g1_i1[2]: 5.56416938462837e-38
c63720_g1_i1[3]: 6.9074104574357e-34
c52697_g1_i3[0]: -19.7121643690573
c52697_g1_i3[2]: 6.95007628588691e-38
c52697_g1_i3[3]: 7.42715989045943e-34
c54027_g2_i2[0]: -19.7096756788425
c54027_g2_i2[2]: 7.07063663549284e-38
c54027_g2_i2[3]: 7.42715989045943e-34
c55424_g1_i2[0]: -14.9238636253627
c55424_g1_i2[2]: 1.67866916859075e-36
c55424_g1_i2[3]: 1.63736191654935e-32
c53640_g1_i1[0]: -18.9525614294271
c53640_g1_i1[2]: 1.34402113000358e-35
c53640_g1_i1[3]: 1.22355203605092e-31
c49936_g1_i1[0]: -18.9066537149149
c49936_g1_i1[2]: 1.84747309451501e-35
c49936_g1_i1[3]: 1.57676055263436e-31
c61346_g1_i2[0]: -14.4532346909692
c61346_g1_i2[2]: 4.37960869176717e-35
c61346_g1_i2[3]: 3.51798508767215e-31
c52697_g1_i4[0]: -18.637842793771
c52697_g1_i4[2]: 1.19009210093315e-34
c52697_g1_i4[3]: 9.02850149127368e-31
c53336_g2_i1[0]: -18.5076080431514
c53336_g2_i1[2]: 2.93451761200646e-34
c53336_g2_i1[3]: 2.10906869740812e-30
c13587_g1_i1[0]: -18.4719269225614
c13587_g1_i1[2]: 3.75786641185474e-34
c13587_g1_i1[3]: 2.56577723935412e-30
c48254_g1_i1[0]: -18.4479382811535
c48254_g1_i1[2]: 4.43827627976478e-34
c48254_g1_i1[3]: 2.88604198753943e-30
c53513_g1_i1[0]: -18.3774595626302
c53513_g1_i1[2]: 7.23348927630069e-34
c53513_g1_i1[3]: 4.48985967329655e-30
c52353_g2_i1[0]: -18.3691781540535
c52353_g2_i1[2]: 7.6593042463829e-34
c52353_g2_i1[3]: 4.54746213636877e-30
c56356_g1_i3[0]: -18.2534296819214
c56356_g1_i3[2]: 1.70868862762047e-33
c56356_g1_i3[3]: 9.72208231436306e-30
c41529_g2_i1[0]: -18.1994639286288
c41529_g2_i1[2]: 2.48348881197431e-33
c41529_g2_i1[3]: 1.35653125887661e-29