Solved

How to catch the number in a line in Perl?

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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
HTTP Error 502.2 - Bad Gateway 3 222
problem with using the glob function 1 74
Perl script to delete older files 6 88
Perl File::Find alternative 1 78
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…

856 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