Solved

Regular Expressions Perl

Posted on 2010-08-19
4
388 Views
Last Modified: 2012-08-14


Hi,

I am trying to parse a date field with format

dd/mm/YYYY HH:MM:SS

sometimes the date has only one digit and sometime the HH has only
one digit
example of input

7/03/2001 9:09:18,034449599,BGC,WCOM,C3400

I want to look at each field of the date if there is only digit I
concatenate a 0 in front of it, example if the day is 7 I put 07,
if the hour is 9 I put 09. I also want to elimnate the space.

so for the input above the desired output

is
07/03/200109:09:18,034449599,BGC,WCOM,C3400

:

I am able to isolate the dd and remove the space but when I am unable
to isolate the mm and the HH

example:

#!/usr/bin/perl

#use strict;

use warnings;
my $inputFile;
my $inputname = "Sample1Date.txt";
my $outputfile;
my $outputname = "Sample1Date_output.txt";
my $line;
my @lineParts;
my @linePartsDay;
my @linePartsMonth;
my $temp2="";

open (inputFile, "$inputname");
#open ($inputFile, "<$inputname") || die "Can not open input file";
open ($outputfile, ">$outputname") || die "Can not open output file";
my $temp = "";

while ($line = <inputFile>)

{

       @lineParts = split '\,', $line;
       my $date = $lineParts[0];
       @linePartsDay = split '\/', $date;
       my $day = $linePartsDay[0];
       print "day : $day\n";

       #@linePartsMonth = split '\^(\d{2}/\d{2}/)', $date;
       @linePartsMonth = split '\^(//)', $date;

       my $month = $linePartsMonth[0];
       print "month : $month\n";


       #$temp2 = $date;
       #$temp2 =~ s/(\s)+//;

       #$date = $temp2;   # remove space(s) in the middle of date
       print "date : $date\n";
       my $number1 = $lineParts[1];
       my $donor = $lineParts[2];
       my $recipient = $lineParts[3];
       my $routing = $lineParts[4];
       $temp = $date.",".$number1.",".$donor.",".$recipient.",".$routing;
       print $outputfile  "$temp";


}

#close $inputFile;

close $outputfile;

I get:

day: 7  (OK) I can then look at the length of day if it is only 1 digit concatenate a 0 in front of it.
month: date: 7/03/20019:09:18
date: 7/03/20019:09:18

I do not know how to isolate the mm, once I get the month I would like to isolate
the yyyy the HH the MM and the SS and to check each field (once I have isolate I
know how to concate).

I tried various regular expressions for isolating the month but unable...

       #@linePartsMonth = split '\^(\d{2}/\d{2}/)', $date;
       @linePartsMonth = split '\^(//)', $date;

       my $month = $linePartsMonth[0];
       print "month : $month\n";

Do you know how to the isolation for MM, YYYY HH MM SS


0
Comment
Question by:Johannne1
[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
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 33481974
$_= '7/03/2001 9:09:18,034449599,BGC,WCOM,C3400';
s/ ?\b(\d)\b/0$1/g;
print;
0
 

Author Comment

by:Johannne1
ID: 33481987
Thanks ozo, I will try this.
0
 
LVL 84

Expert Comment

by:ozo
ID: 33482002
($DD,$MM,$YYYY,$HH,$MM,$SS,$parts)=split/\D+/,'7/03/2001 9:09:18,034449599,BGC,WCOM,C3400',7;
printf "%02d/%02d/%04d%02d:%02d:%02d,%s",$DD,$MM,$YYYY,$HH,$MM,$SS,$parts;
0
 

Author Comment

by:Johannne1
ID: 33482047
Beautiful! 1 line!! you are a regular expression master!
I did:
$_= $line;
s/ ?\b(\d)\b/0$1/g;
print"date : $_\n";

and tried different data and the 0 when 1 digit. I am not sure about the first s . s/
does thhis mean string and then is there a backslash followed by a b what does the b stand for
0

Featured Post

Technology Partners: 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!

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

726 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