Solved

How to catch the number in a line in Perl?

Posted on 2013-05-22
3
302 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
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 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 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now