Perl: How to find digits folloed by specific string

ambuli
ambuli used Ask the Experts™
on
Hi there,

I have a string like:

"This thread took 90ms and something here."
I want to check a file for lines like this and extract the time value. The time value will always be followed by ms.

How can I do it?

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
my string = "This thread took 90ms and something here.";
#if you don't want ms in time value
my ($time) = $string =~ m/\s+(\d+)ms/;

#And if you want ms in time value
my ($time) = $string =~ m/\s+(\d+ms)/;

Author

Commented:
Thanks farzanj:
And how can I test if the line actually have a [digits]ms as a word?
Like this:


if ( $string =~ m/\s+(\d+ms)/)
{
    print "Number exists";
}
if ($string =~ m{\s+(\d+ms)\b}) {
    $time = $1;
    # do whatever else you want to do when digits followed by ms exists
}

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