We help IT Professionals succeed at work.
Get Started

Need perl script to extract real numbers from a string

Phazz asked
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.

Watch Question
Most Valuable Expert 2014
Top Expert 2015
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE