Solved

How to catch the number in a line in Perl?

Posted on 2013-05-22
3
296 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 74

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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

18 Experts available now in Live!

Get 1:1 Help Now