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

Hi,
Sorry if I am asking a dumb question....I am new to Perl. I have a file containing the following:
HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2") GENRE_BY("dumpTc") ) ... ... HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2") GENERATED_BY("dumpTc") ) ... ... HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2.1") GENRE_BY("dumpTc") ) ... ...

I want to capture any part of the text that has the following exact sequence of lines- anywhere it occurs in the ascii file I have:
HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2") GENRE_BY("dumpTc") )

Or
HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2.1") GENRE_BY("dumpTc") )

and print it to stdout. For this, if I use perl's range operator, everything works fine.
But if I use something as :
... if(/^HEADER(.*?)GENRE_BY(.*)/) { print; } ...
I am not able to get the RE to match:
<code> HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2") GENRE_BY("dumpTc") )
Or
HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2") GENRE_BY("dumpTc") )

Can you please point out my mistake?
Christina J.

Replies are listed 'Best First'.
Re: Question on RE based matching
by BrowserUk (Patriarch) on Nov 19, 2007 at 02:41 UTC

    You want the regex to match across multiple lines, so you will need to use /m so that ^ will match at the start of any line in the string, and /s so that . will match newlines. Ie. try:

    if(/^HEADER(.*?)GENRE_BY(.*)/sm) { print; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Question on RE based matching
by GrandFather (Saint) on Nov 19, 2007 at 02:27 UTC

    A code sample would help to see what you are doing. The following may be useful as either a seed for a sample, or may help solve the problem (I can't tell which ;) ):

    use strict; use warnings; local $/ = 'HEADER('; while (<DATA>) { chomp; next unless /(.*\bGENRE_BY\b\(.*\)\s*\))/s; print "HEADER($1"; } __DATA__ HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2") GENRE_BY("dumpTc") ) other stuff here HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2.1") GENRE_BY("dumpTc") )

    Prints:

    HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2") GENRE_BY("dumpTc") )HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2.1") GENRE_BY("dumpTc") )

    Perl is environmentally friendly - it saves trees
Re: Question on RE based matching
by almut (Canon) on Nov 19, 2007 at 02:27 UTC

    You probably want the s modifier to the match, so the dot in .* will also match the newlines — i.e. if (/ ... /s) {.  (In addition to that, you have to make sure that you have the entire HEADER(...) entry read into the string you're matching against, e.g. by setting the input record separator $/ appropriately, or by accumulating the lines starting from 'HEADER(' up to ')' ...)

Re: Question on RE based matching
by ww (Archbishop) on Nov 19, 2007 at 01:45 UTC
    I'm curious about use of the range operator; I think most of us can be more helpful if we see the code for the regex version. Pls update your post with code.

    But for starters, what you do show:

    if(/^HEADER(.*?)GENRE_BY(.*)/

    isn't a regex. Did you mean something like this?

    if ($somevar =~ /^HEADER(.*?)GENRE_BY(.*)/) ...
      if(/^HEADER(.*?)GENRE_BY(.*)/
      isn't a regex.

      You left out the close paren (for the "if" condition) that was in the OP, but apart from that, it is a regex. It is doing a match on $_, which is a normal and sensible thing to do.

      Hi ww,
      Here is the complete code:
      #!/usr/bin/perl use strict; use warnings; our ($fileIn, $fileOut); use Getopt::Long; GetOptions( 'FileIn=s' => \$fileIn, 'FileOut=s' => \$fileOut, ); if($fileIn && $fileOut) { open(INPUT,"<","$fileIn"); #undef $/; while(<INPUT>) { next if /^\n/; s/\t+//g; s/^\s+//g; if(/^HEADER(.*?)^GENRE_BY(.*)/sm) { print ; } } }

      I saved this code into a file called x.pl. Made x.pl into an excutable code.
      I executed, ./x.pl -fileIn lla -fileOut out_file
      where the input file lla contains:
      HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2") GENRE_BY("dumpTc") ) ... ... HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2") GENERATED_BY("dumpTc") ) ... ... HEADER( LIBDAT("GTASK") VENDOR("DeltaQ") Environment("zeronom") TASK_VERSION("5.2.1") GENRE_BY("dumpTc") ) ... ...

      I am possibly doing something wrong. Any help will be great!
      Christina J.

        Your (main) problem is that you're matching against individual lines, i.e. 'HEADER(', next '  LIBDAT("GTASK")', and so on. In other words, the regex can never find 'HEADER...' and 'GENRE_BY...' at the same time.

        One way around this would be to split the input appropriately (as shown in the snippet by GrandFather). Another way would be to use the flip-flop operator .. to accumulate the necessary lines as shown below. The expression if (/^HEADER/ .. /^\)/) is true starting with a line matching /^HEADER/ up until a line matching /^\)/, in which case you append the current line to the accumulator ($entry). Upon the next line (not being HEADER or )) the flip-flop is reset to false.

        (I've put in an additional end-of-entry test (if (/^\)), so the code will correctly deal with the case where a new HEADER line is immediately following a previous entry (without any other lines in between) — if your input always has other stuff in between the entries, you could also just put the regex test directly in an else branch of the flip-flop test...)

        #!/usr/bin/perl use strict; use warnings; use Getopt::Long; GetOptions( 'FileIn=s' => \my $fileIn, 'FileOut=s' => \my $fileOut, ); if ($fileIn && $fileOut) { open(INPUT,"<","$fileIn"); my $entry = ""; while (<INPUT>) { next if /^\n/; if (/^HEADER/ .. /^\)/) { # flip-flop $entry .= $_; # accumulate input } if (/^\)/) { # end of entry # check for desired type of entry if ($entry =~ /^HEADER(.*?)^\s*GENRE_BY(.*)/sm) { print $entry; } $entry = ""; # reset accumulator } } }