Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Regular Expressions Perl

Posted on 2010-08-19
4
Medium Priority
?
392 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

636 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