Link to home
Create AccountLog in
Avatar of Phazz
Phazz

asked on

Need perl script to extract real numbers from a string

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!
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
echo "foo 11 bar 2. bla -3.33 4.5e-06 text78text +9." | perl -MRegexp::Common -lne 'print join" ",/($RE{num}{real})/g'
echo "foo 11 bar 2. btext +9." | perl -MScalar::Util=looks_like_number -lane 'print "@{[grep looks_like_number $_,@F]}"'
Avatar of Phazz
Phazz

ASKER

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.
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
Avatar of Phazz

ASKER

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