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

I have the following code
print ("<b>$1</b> $4\n",br) if ( m/(^....$MONTH..+$DAY.$HOUR:$MINUTE:. +.(:..)+.(20..))(.*)/ );
This works fine for lines like:
Tue Apr 12 03:00:46 2005 Event on Socket 2 Begin Processing Client
but doesn't match at all on lines like:
Thu Apr 7 03:00:39 2005 Event on Socket 2 Begin Processing Client
Note the two spaces between Apr and 7, yet only one between Apr and 12. Ive gotta be missing something obvious here.

Thanks in advance
Ted

Replies are listed 'Best First'.
Re: pattern matching whitespace
by ikegami (Patriarch) on Apr 13, 2005 at 16:35 UTC

    Two ways come to mind:

    1) Preformat your variables correctly:

    1a)

    $day = sprintf('%2d', $DAY); $hour = sprintf('%02d', $HOUR); $minute = sprintf('%02d', $MINUTE); print("<b>$1</b> $2\n", br) if m/^(... $MONTH $day $hour:$minute:.. .{4}) (.*)/;

    1b)

    # Thu Apr 7 03 : 00 :39 2005 $time_stamp = sprintf('... %s %2d %02d:%02d:.. ....', $MONTH, $DAY, $HOUR, $MNUTE, ); print("<b>$1</b> $2\n", br) if m/^($time_stamp) (.*)/;

    2) Extract the parts and compare those:

    print("<b>$1</b> $6\n", br) # Thu Apr 7 03 : 00 :39 2005 if m/^(... (...) (..) (..):(..):.. ....) (.*)/ && $2 eq $MONTH && $3 == $DAY && $4 == $HOUR && $5 == $MINUTE;

    I prefer #2

Re: pattern matching whitespace
by ww (Archbishop) on Apr 13, 2005 at 16:43 UTC
    The "something" seems to be reading about quantifiers within regex. Quantifiers in regular expressions includes a nice little table in root's reply. There are many nodes dealing with regexen generally, qv, but best might be to read the docs... including perlretut, etc...

    HTH

Re: pattern matching whitespace
by sh1tn (Priest) on Apr 13, 2005 at 16:06 UTC
Re: pattern matching whitespace
by davidrw (Prior) on Apr 13, 2005 at 16:41 UTC
    in general, if it's unknown number, you can can use the ? or {} modifiers.. for example, any of the following would catch 1 or two spaces:
    /( | )/ / ?/ / {1,2}/
    Where x? matchs 0 or 1 x, and x{n,m} matches between n and m x's, inclusive. A couple general comments.. -- it's more robust (maybe not necessary for your task) to not use just a "." when the posible characters are known. for example, match "/20\d\d/" instead of "/200../" and "/^a-zA-Z{3} /" instead of "/^..../" (or even "/^(?:Mon|Tue) /" type of thing).
      using /( |  )/ seems to do the trick. I don't know why I was hung up on using /..?/ or /.+/

      Thanks
      Ted
        The problem was that you used /..+/, which means "two or more", not "one or more" or "one or two".