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

Hello All,

Would like to know what is wrong with the code (below) that I wrote to extract group of lines that match a set of patterns (including the lines that match the pattern)

code is here:

if ($_ =~ /^E/gi || $_ =~ /gmake.*/gi) { push (@errors, $_) }

and the lines from which I need to extract text is here:

I have updated since the lines do not start with that special character (carat) at the line beginning (as posted intially)

E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) gmake : **** build failed fdjgu43uffer Error 1

With this code, am able to extract the last line only (i.e. the one starts with ^E).

Could anyone tell me what is wrong with regular expression ?

thanks in advance

/Ram

Edited by planetscape - swapped out some p tags for some code tags

Replies are listed 'Best First'.
Re: Extraction of set of lines using regular expression
by ikegami (Patriarch) on Apr 28, 2006 at 19:47 UTC

    [ This is my new answer to the new (i.e. updated) question. ]

    Style issues aside, there's nothing wrong with your code. It works for me with no changes. For example,

    while (<DATA>) { if ($_ =~ /^E/gi || $_ =~ /gmake.*/gi) { push (@errors, $_) } } print(@errors); __DATA__ E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) E : abcdfcsds 1387382 43243 ... E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) gmake : **** build failed fdjgu43uffer Error 1
    outputs
    E : abcdfcsds 1387382 43243 E : abcdfcsds 1387382 43243 E : abcdfcsds 1387382 43243 gmake : **** build failed fdjgu43uffer Error 1

    Are you saying you are getting something different?

    I said "style issues aside" earlier, because your code stands to be improved. $_ =~ is unnecessary, /i is unnecessary here, /g is even more useless, and your regexps would be more robust if they were more restrictive, as follows:

    while (<DATA>) { if (/^(?:E|gmake)\s*:/) { push(@errors, $_); } }
Re: Extraction of set of lines using regular expression
by TedPride (Priest) on Apr 28, 2006 at 20:09 UTC
    The g flag is unnecessary, since you only need to match once. You also don't need two expressions, since you can combine them into one, and if the value is in $_, you don't have to specify it in the expression. The following does essentially the same thing:
    while (<DATA>) { push @errors, $_ if m/^E|gmake/i; } print @errors; __DATA__ E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) E : abcdfcsds 1387382 43243 ijigje jg445 3u4 (53545545) gmake : **** build failed fdjgu43uffer Error 1
    Or if you want it to match only when gmake is at the start as well, and just made a typing error:
    push @errors, $_ if m/^(?:E|gmake)/i;
Re: Extraction of set of lines using regular expression
by ikegami (Patriarch) on Apr 28, 2006 at 19:28 UTC

    [ The question was updated. This post is no longer relevant. ]

Re: Extraction of set of lines using regular expression
by eff_i_g (Curate) on Apr 28, 2006 at 19:56 UTC
    Was the initial posting of "^E" really a control-E? Otherwise, I don't see why the carets were added and then removed, and why ikegami's code did not solve the issue. Just wondering...