#!/usr/bin/perl use warnings; use strict; my $data = q|iters = 320 (3.05s) aver = 9568.34 us (9603.45 us, 1.01s, 99.79%)|; my @numerics; @numerics = ($data =~ /([\d\.]+)/g); print "*$_*\n" for @numerics;Output:
---------- Capture Output ---------- > "c:\perl\bin\perl.exe" _new.pl *320* *3.05* *9568.34* *9603.45* *1.01* *99.79* > Terminated with exit code 0.
m/([+\-]?[0-9\.]+)/g;Using your example, and putting this into a short script:
#!/usr/bin/perl -w
use strict;
use Data::Dumper::Simple;
my @numbers;
while () {
@numbers = $_ =~ m/([+\-]?[0-9\.]+)/g;
}
print Dumper(@numbers);
__DATA__
iters = 320 (3.05s) aver = 9568.34 us (9603.45 us, 1.01s, 99.79%)
Which gives:
@numbers = (
'320',
'3.05',
'9568.34',
'9603.45',
'1.01',
'99.79'
);
Does this help?
Cheers,
Darren :)
I would like to know how to handel numerical regularguar expressions of this type?
You might find that building up the pattern piece-by-piece will help you get a handle on constructing the final pattern.
Given:
iters = 320 (3.05s) aver = 9568.34 us (9603.45 us, 1.01s, 99.79%)
The trickiest part matching a number with a decimal component, which is one or more digits, followed by a literal dot, and one or more digits. Which gives:
my $num = qr/\d+\.\d+/;
If you want to match positive or negative numbers, you would have to prefix that with an optional character class:
my $num = qr/[-+]?\d+\.\d+/;
But given the sample data, I'm not sure that that's even possible. But nonetheless, with this approach, which ever way you want to go doesn't really matter any more, because you just interpolate either of the above patterns into your overall pattern, which gives:
/iters = \d+ \(($num)s\) aver = ($num) us \(($num) us, ($num)s, ($num)%\)/
And you're done.
• another intruder with the mooring in the heart of the Perl
but... point 2:
As written, the "\" in your regex before each open_square_bracket escapes the bracket, meaning you're NOT matching against a character class, but rather against a string consisting of open_sq, +, -, close_sq. before the digits.
and, point 3:
Perl's implementation of RE is (ok, "is arguably") the best around, but perl is not the only language with regexen; javascript and java leap immediately to mind, and various *nix utilities implement them in more limited fashion (and with some variance of syntax)
Online tutorials are numerous, perlretut and friends work well as quick refreshers, and the gold standard is Friedl's book, "Mastering Regular Expressions." (The link goes to O'Reilly's twofer (as of April 06) offer; the Stibblebine "Pocket Reference"
if (ln =~ /(\[+-]d) +(\[+-]d.d) +(\[+-]d.d) +(\[+-
+]d.d) +(\[+-]d.d) +(\[+-]d.d)/) { do something}
Here's where you went astray:
\d matches a digit. \[+-]d.d matches an open_square_bracket, a plus, a minus, a close_square_bracket, a lowercase "d", any single character except newline, and another lowercase "d".
If you want to match "3.4" or a similar x.x format number, you'd want \d\.\d.
If you want to match a number, you can use one of the regexes in How do I determine whether a scalar is a number/whole/integer/float? (You'll want to remove the anchors and manage the capturing parens, see below.) Or you could use Regexp::Common.
It's a good idea to go read the Regular Expression Reference and the Regular Expression Tutorial.
-QM
--
Quantum Mechanics: The dreams stuff is made of
perlmonks.org content © perlmonks.org and alejack12001, grinder, McDarren, puudeli, QM, wfsp, ww
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03