Re: Date parsing
- From: Gunnar Hjalmarsson <noreply@xxxxxxxxx>
- Date: Fri, 30 Dec 2005 23:01:39 +0100
glvs@xxxxxxxxxxx wrote:
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...
-- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl .
- Follow-Ups:
- Re: Date parsing
- From: glvs
- Re: Date parsing
- References:
- Date parsing
- From: glvs
- Date parsing
- Prev by Date: Re: 'file changed'-event
- Next by Date: FAQ 4.74 How do I define methods for every class/object?
- Previous by thread: Date parsing
- Next by thread: Re: Date parsing
- Index(es):