We help IT Professionals succeed at work.

Need perl script to extract real numbers from a string

Phazz
Phazz asked
on
Medium Priority
389 Views
Last Modified: 2012-05-12
Hey Perl experts!
I need a perl script to extract only the numbers from a given text string. As simple as possible, please. I don't know if perl has something build-in or I need a regular expression to do it.

EDIT: Perhaps perl is not even the best to use for this purpose? I am not bound to use perl.

The matched numbers should be valid real numbers, but they can have all kinds of normal or tricky forms, including negative, floating point, exponential notations, as indicated by this example:

echo "foo 11 bar 2. bla -3.33 4.5e-06 text78text +9." | extract_numbers.pl

The output should be the following:

11 2. -3.33 4.5e-06 78 +9.

Thanks!
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
echo "foo 11 bar 2. bla -3.33 4.5e-06 text78text +9." | perl -lne 'print join" ",/([-+]?\d+\.?\d*(?:e[-+]\d+)?)/g'
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
echo "foo 11 bar 2. bla -3.33 4.5e-06 text78text +9." | perl -MRegexp::Common -lne 'print join" ",/($RE{num}{real})/g'
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
echo "foo 11 bar 2. btext +9." | perl -MScalar::Util=looks_like_number -lane 'print "@{[grep looks_like_number $_,@F]}"'

Author

Commented:
Thanks! The first solution works perfectly, great! I will accept that solution.

Howerever my perl installation does not seem to support the other two solutions:

==========================================================================
echo "foo 11 bar 2. bla -3.33 4.5e-06 text78text +9." | perl -MRegexp::Common -lne 'print join" ",/($RE{num}{real})/g'

Can't locate Regexp/Common.pm in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 .).
BEGIN failed--compilation aborted.
============================================================================
echo "foo 11 bar 2. btext +9." | perl -MScalar::Util=looks_like_number -lane 'print "@{[grep looks_like_number $_,@F]}"'

"looks_like_number" is not exported by the Scalar::Util module
Can't continue after import errors at -e line 0
BEGIN failed--compilation aborted.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Hmm, looks_like_number should have been in Scalar::Util  in 5.8.0
what do you see at the beginning of
perldoc Scalar::Util

Author

Commented:
Well, it prints something. What should I look for? Here are the first few lines, and I attach the full output as a text file.

Scalar::Util(3)       User Contributed Perl Documentation      Scalar::Util(3)

NAME
       Scalar::Util - A selection of general-utility scalar subroutines

SYNOPSIS
           use Scalar::Util qw(blessed dualvar isweak readonly refaddr reftype tainted weaken);

DESCRIPTION
       "Scalar::Util" contains a selection of subroutines that people have
 
perldoc.txt