in reply to Re: Find a pattern
in thread Find a pattern

All,
Thanks a lot
My requirement is such that if the log contains the string
like "2 failed" or "3 failed"..."<any number except 0>" failed.
I have some other processing to do. So will the below code work.

my $file = "D:/perl/02.log"; open my $inFile, '<', $file or die "cant open $file: $!"; # Look for errors while (<$inFile>) { if (/\d{1-9} failed/) { print "There are build errors (see line $.)\n"; exit 0; } }

Thks AV

Replies are listed 'Best First'.
Re^3: Find a pattern
by GrandFather (Saint) on Jan 28, 2011 at 11:13 UTC

    It's a slightly tricky match to get right depending on just how fussy you really want to be. Using a negative look ahead helps cover most bases though. Consider:

    use strict; use warnings; my $str = <<STR; Errors: 0 failed Errors: 01 failed Errors: 1 failed Errors: 10 failed Errors: None failed Ben10 failed STR open my $fIn, '<', \$str; while (<$fIn>) { print if /\b(?!0+\b)\d{1,9} failed/; }

    Prints:

    Errors: 01 failed Errors: 1 failed Errors: 10 failed

    The (?!0+\b) is the negative look ahead that fails the match if a zero value (regardless of the number of 0 digits) is found. The \b's are there to ensure all the digits in the number are matched.

    True laziness is hard work
Re^3: Find a pattern
by jethro (Monsignor) on Jan 28, 2011 at 10:25 UTC
    Won't work. Use /[1-9] failed/ for that. \d{1-9} would actually parse a digit followed by '{1-9}' or a number with 1 to 9 digits if the '-' were a ','. More info in perlre.