Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to catch the last number in a string in Perl?

How can I catch the "27" in the following string in Perl?


The McCabe complexity of 'myFile' is 27.

Open in new window



The general question is like, how to catch XX in the following string

The McCabe complexity of 'filename' is XX.

Open in new window

Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Well, since they're the only numbers in that line, just look for digits:

$line = "The McCabe complexity of 'myFile' is 27.";
$line =~ m/.*?(\d+).*/;
$number = $1;
print $number, "\n";

Open in new window

Are there going to be any numbers *before* the "27" in this line?
Avatar of Tolgar
Tolgar

ASKER

The filename may include numbers. Or the worst case scenario is the file name can be only numbers. But I guarantee that the file name will be in single quotes.
$line = "The McCabe complexity of 'myFile2' is 27.";
$number =  (split/\D+/,$line)[-1];
Avatar of Tolgar

ASKER

And the other thing is, I read a file into a string. So When we search for the number we should also check that it has this patttern:

The McCabe complexity of 'someTextInHere' someNumberInHere

Open in new window


So the string will be some thing like this:

some characters in here and some other characters
in here then we also have stuff in here then we have The McCabe complexity of 'myFile' 43
and we also have characters in the file.

Open in new window


How are going to catch 43 in this long string?

I guarantee that we will have "The McCabe complexity of 'SOMEFileNAME' " before the number.
ASKER CERTIFIED SOLUTION
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial