• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Regular Expressions Perl


I am trying to parse a date field with format


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



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



#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

  • 2
  • 2
1 Solution
$_= '7/03/2001 9:09:18,034449599,BGC,WCOM,C3400';
s/ ?\b(\d)\b/0$1/g;
Johannne1Author Commented:
Thanks ozo, I will try this.
($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;
Johannne1Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now