问题
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