Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to catch the number in a line in Perl?

Posted on 2013-05-22
3
Medium Priority
?
312 Views
Last Modified: 2013-05-22
Hi,
How can I catch the number in a line which look like the following:

The example input is:

33c33

Open in new window


The expected output for this example is:

33

Open in new window


Thanks
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 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 39187986
($number) = ($source =~ /(\d+)/);

Open in new window

0
 

Author Comment

by:Tolgar
ID: 39188128
Can you also tell me how I can tell if the line has this format:

33c33 

Open in new window


which is:

$numberc$number

Open in new window


I try to catch this line and then capture the number in it as you told me in the previous post.

So it will be something like this:

my $catch = 0
foreach my $line (@fileContent){

if($line STARTS WITH diff){
   $catch = 1;
}

if ($catch =1 AND $line =~ SOME REGULAR EXP TO CATCH IF LINE HAS THE EXPECTED FORMAT){
    ($number) = ($LINE =~ /(\d+)/);
}

}

Open in new window

0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 1000 total points
ID: 39188175
Your description of what you want is not real clear, but if you want to extract both numbers, there are several ways to accomplish that; here's one.

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $str = '33c32';

my ($number_c, $number) = $str =~ /(\d+)/g;
print Dumper($number_c, $number);

Open in new window


You could also use this regex.
/(\d+)c(\d+)/

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

721 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