Date parsing

G

glvs

Hi,
I need to split the input data based on lines that start with a valid
date.

Example:

Input:

1/2/03:note 1
note 2 note 3
01/2/2004 another note
note line2
note line3
5-03-99 this is a note from 1999

Output:

Date: 1/2/03
note1
note2 note3

Date: 01/2/2004
another note
note line2
note line3

Date: 5-03-99
this is a note from 1999

This is tricky because dates can be in different formats (1 digit month
instead of 2, for example). Is there any easy way to do this in Perl?

Thanks,
Glenn
 
G

Gunnar Hjalmarsson

I need to split the input data based on lines that start with a valid
date.

Example:

Input:

1/2/03:note 1
note 2 note 3
01/2/2004 another note
note line2
note line3
5-03-99 this is a note from 1999

Output:

Date: 1/2/03
note1
note2 note3

Date: 01/2/2004
another note
note line2
note line3

Date: 5-03-99
this is a note from 1999

This is tricky because dates can be in different formats (1 digit month
instead of 2, for example). Is there any easy way to do this in Perl?

Without a strict definition of a valid date, I can't think of a safe
way. This might be something to start with, assuming the input is in $_:

my ($date, $lastpos);
while ( /(\d{1,2}([\/-])\d{1,2}\2\d{2,4})./g ) {
if ($date) {
print "Date: $date\n";
print substr($_, $lastpos, pos() -
$lastpos - 1 - length $1 ), "\n";
}
$date = $1;
$lastpos = pos;
}
print "Date: $date\n";
print substr($_, $lastpos), "\n";

Of course, you may want to further check that respective date is
actually a valid date, e.g. that month is one of 1 - 12. But before that
you need to decide if month is the first or second number...
 
E

Eric J. Roode

(e-mail address removed) wrote in @g43g2000cwa.googlegroups.com:
Hi,
I need to split the input data based on lines that start with a valid
date.

Example:

Input:

1/2/03:note 1
note 2 note 3
01/2/2004 another note
note line2
note line3
5-03-99 this is a note from 1999

Output:

Date: 1/2/03
note1
note2 note3

Date: 01/2/2004
another note
note line2
note line3

Date: 5-03-99
this is a note from 1999

This is tricky because dates can be in different formats (1 digit month
instead of 2, for example). Is there any easy way to do this in Perl?

use Regexp::Common qw(time);

$input =~ /^$RE{time}{mdy}/;


Then you can use Time::Normalize to validate the results....

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top