Perl REGEX repetive capture groups when post processing output file

旧巷老猫 提交于 2019-12-11 03:45:21

问题


I am dealing with a string of numbers in scientific format. for example

24  6.924E+06  8.316E-01  1.052E-01  1.622E+01  1.311E+01  0.000E+00  6.059E-06 (snip.. extends for a bit)

Now I want to write a regex for perl which allows me to capture the ith value in the list. So my current set up is the folloiwng

$_ =~ ^\s+\d+\s+(\d+[.]\d+E[+]\d+);
my $temp = $1;

Which will get me the first number. I want to be able to capture the 7th or the 50th if I wanted without having to write a really long regex expression.

is there a concise way of doing this?

Thanks in advance.


回答1:


use split

my @cols = split ' ', $_;

my $seventh = $cols[6];
my $fiftieth = $cols[49];



回答2:


split is the best option for this case.

my @val = split ' ', $_;
my $val7 = $val[6];



回答3:


An example for the third number:

^\s+\d+(?:\s+(\d+\.\d+E[-+]?\d+)){3}

When you repeat a capture group, the content is overwritten with the last match.

NB: in this case, it is more clean to use [0-9] instead of \d since you only need arabic digits.



来源:https://stackoverflow.com/questions/24833934/perl-regex-repetive-capture-groups-when-post-processing-output-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!