Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am trying to match against this Thu Feb 3 14:32:52 2005 in the text file below, the file will have multiple entries like the block of data here, and I need to match against the time stamp so the user wouldn't entry stuff twice, it seems that my regular expression isn't working, can someone tell what is the best way to do it?
Thank you!
@:1107459172::1 MORe TEXT <br>okokoko <br>TEXT MORE TITLE twisted.gif Thu Feb 3 14:32:52 2005

open(DATA,"db.txt") or die $!; while(<DATA>){ chomp; my @array=$_; if(@array=~/(.*)(\d+:\d+:\d+)(.*?)/g) do code... }

20050204 Edit by castaway: Changed title from 'Reg. Ex.p Match'

Replies are listed 'Best First'.
Re: Using regex to match date and time
by friedo (Prior) on Feb 03, 2005 at 20:21 UTC
    You are trying to do a regex match on an array, which doesn't really make sense. You're also only reading a single line into your array. Finally, by declaring your array as a lexical (my) variable inside your while loop, it immediately goes out of scope when the loop ends.

    These are just a few of the reasons why you should be using strict and warnings. These problems must be fixed before you bother with the regex.

    It looks like you're trying to read the file one line at a time and do something if it matches. I suggest the following as a general framework:

    use strict; use warnings; open DATA, "db.txt" or die $!; my @array; while(<DATA>) { chomp; # save the line in our array push @array, $_; # see if it matches if ($_ =~ /(.*)(\d+:\d+:\d+)(.*?)/g) { # do code } }
      Thanks, it worked, I need to read more!
Re: Using regex to match date and time
by Fang (Pilgrim) on Feb 03, 2005 at 20:21 UTC
    You probably haven't turned warnings on, as perl would've told you "Applying pattern match (m//) to @array will act on scalar(@array) at foo.pl line 17".

    Your regex looks fine for what you want to do, but your while should be more like that:
    while (<DATA>) { chomp; next unless /^(.*)(\d+:\d+:\d+)(.*?)$/; # if you're here, there was a match # do what you want with $1, $2 and $3 ... }
Re: Using regex to match date and time
by ww (Archbishop) on Feb 03, 2005 at 20:38 UTC

    Beyond the unarguable issues noted above re lack of warnings, misuse of the array and the while...
    as the regex itself is written, you have three captures, (.*), (the digits and colons) and (any_old_junk lazily).
    Since the digits capture is greedily quantified, it could match on variant data: (for example: 243:4321:78654321) that was not actually part of a datestamp.

    ...So it might be well to assure yourself that item is formatted as datetime

    [A-Z][a-z]{2} [A-Z][a-z]{2}\s\s?\d\d?: ^exactly 1 space ^one or more spaces,
    or somesuch....