Question : Regular Expressions Perl



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


Answer : Regular Expressions Perl

$_= '7/03/2001 9:09:18,034449599,BGC,WCOM,C3400';
s/ ?\b(\d)\b/0$1/g;
print;
Random Solutions  
 
programming4us programming4us