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

Greetings all,
open(LOG,"test.txt"); my $pattern = '(\s+\w+\s+\d{2}\s\d{4}) | (Release\s+\d+\.\d+)'; while(<LOG>) { print $1 if($_ =~ /($pattern)/ ); }

My  test.txt contains
__DATA__ December 15 2005 some line Flat File Release 100.0 someline
I can't able to get the 'December 15 20005' value. please suggest any other pattern or why it's not returning?. I guess, either one of the pattern should match and it'll reflect in the  $1.

-kulls

2005-12-20 Retitled by g0n, as per Monastery guidelines
Original title: 'RegExp'

Replies are listed 'Best First'.
Re: Regexp for extracting date
by tirwhan (Abbot) on Dec 20, 2005 at 09:45 UTC

    You need to either use the "x" modifier for your match (so that it ignores whitespace in the pattern) or remove the space between your first pattern and the |. Also, you're requiring whitespace before the date, but there is none in your data. This will work:

    my $pattern = '(\s*\w+\s+\d{2}\s\d{4}) | (Release\s+\d+\.\d+)'; while(<LOG>) { print $1 if($_ =~ /($pattern)/x ); }
    and so will this:
    my $pattern = '(\s*\w+\s+\d{2}\s\d{4})|(Release\s+\d+\.\d+)'; while(<LOG>) { print $1 if($_ =~ /($pattern)/ ); }

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      hi,
      Thanx a lot.your 'x' modifier works fine.

      -kulls
Re: Regexp for extracting date
by McDarren (Abbot) on Dec 20, 2005 at 10:23 UTC
    Hi,

    Regexp::Common::time would seem to be made to measure for this task:

    #!/usr/bin/perl -w use strict; use Regexp::Common qw(time); while (<DATA>) { print if ($_ =~ $RE{time}{strftime}{-pat => '%B %d %Y'}); } __DATA__ December 15 2005 some line Flat File Release 100.0 someline

    Which prints:

    December 15 2005

    Cheers,
    Darren :)

Re: Regexp for extracting date
by prasadbabu (Prior) on Dec 20, 2005 at 09:48 UTC

    kulls try this,

    my $pattern = '(\s*\w+\s+\d{2}\s*\d{4})|(Release\s+\d+\.\d+)';# to mat +ch only date #my $pattern = '((\s*\w+\s+\d{2}\s*\d{4})|(Release\s*\d+\.\d+))';# to +match both date and release while(<DATA>) { print "$1\n" if($_ =~ /$pattern/ ); }

    Prasad