Perl: How to find a numric value preceding a string

ambuli used Ask the Experts™
Hi Experts,

I need to find the number in the following string.

THREAD_NAME: spent time 34.4 (units %)
I want to get the 34.4 from this line.  The numeric value will always precede the (units %) string.  But the other words in the string will vary.

Thank you.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
my ($number) = $string =~ m/([0-9.]+)/;
You can also try this one:

my ($number) = $string =~ m/([0-9.]+)\s*\(units/;

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial