Solved

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

Posted on 2013-06-28
8
288 Views
Last Modified: 2013-06-28
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

0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 39284878
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

0
 
LVL 23

Expert Comment

by:nemws1
ID: 39284884
Are there going to be any numbers *before* the "27" in this line?
0
 

Author Comment

by:Tolgar
ID: 39284904
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 39284907
$line = "The McCabe complexity of 'myFile2' is 27.";
$number =  (split/\D+/,$line)[-1];
0
 

Author Comment

by:Tolgar
ID: 39284917
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.
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 167 total points
ID: 39284936
Try this, going back to my first suggestion (ozo's is better if you indeed just want the last number from a line of text:)

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

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 166 total points
ID: 39284950
($number) = "The McCabe complexity of 'myFile' 43" =~ /The McCabe complexity of '.*' (?:is )?(\d+)/;
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 167 total points
ID: 39285058
I'd do a slight modification of nemws1 and ozo's answer.  With both of theirs, it will be incorrect if there is a single quote followed by a number later in the same line (however, mine won't work if there can be single quotes in the filename itself).
$line = "there may be some other stuff here The McCabe complexity of 'myFile' is 27.";
($number) = $line =~ m{The McCabe complexity of '[^']*'(?: is)? (\d+)};

Open in new window

Is it accurate that the is may not always be present?  You don't say so explicitly (unless I missed it) but your example of the long string does not have the "is".  If it is not optional then you can change "(?: is)?" into just plain " is".
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 110
Search in text file in column and compare 4 75
pipe to sed or perl, please help 5 144
Union rows in array that have common elements (Part 2) 4 94
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question