Jeff Westman wrote:
All,

Hello,

I know this has been asked many times, and I have read the documentation
("perldoc -q "matching over more than one line") and still can't make head
or tails out of this.

I have a problem where my pattern can be in one line, or span multiple
lines.  This is what I have so far (simplified):


#!/bin/perl
use warnings;
use strict;
$/ = '';

perldoc perlvar
<QUOTE>
Setting [$/] to "" will treat two or more consecutive empty lines as a single empty line.
</QUOTE>

Where "empty line" means a line with only a newline on it.


my $pat = << 'EOF';
^
    AAA
    (.*)?
    ZZZ
$
EOF

You are defining your pattern as "^\n AAA\n (.*)?\n ZZZ\n$\n". This will not do what you want without the /x option:

my $pat = qr/
^
    AAA
    (.*)?
    ZZZ
$
/xms;


my $file = "./mytext";
while (<DATA>) {
    if ( /$pat/gms ) {
       print "vvvvvvvvvvvvvvvvvv\n";
       print "FOUND: $_";
       print "^^^^^^^^^^^^^^^^^^\n";
    }
    else {
       print "vvvvvvvvvvvvvvvvvv\n";
       print "NOT FOUND: $_";
       print "^^^^^^^^^^^^^^^^^^\n";
    }
}
__DATA__
AAAthis is the beginning of our text
and it will continue over a few lines.
In case you are not sure of what you
see, you should check the document
yourself.ZZZ
This part has nothing to do whatsoever
with the above text, but to be sure,
you should not see this.
AAABut this single line you should seeZZZ
AAABut this double line, so the
question is do you see itZZZ
This part you will not see.

Your data has one paragraph and (.*)? is greedy so it should match the first 'AAA' to the last 'ZZZ'.


I am not sure if I can use $/ to gobble up a paragraph, since we are reading
and parsing XML files, which are around 10M in size.  I need to do a
non-greedy pattern match.


Can someone tell me what I am doing wrong please?

Perhaps you should set $/ to "ZZZ\n":

$/ = "ZZZ\n";
while ( <DATA> ) {
    if ( s/.*?AAA//s ) {
        chomp;   # remove trailing "ZZZ\n"
        print
            "vvvvvvvvvvvvvvvvvv\n",
            "FOUND: $_",
            "^^^^^^^^^^^^^^^^^^\n";
        }
    else {
        print
            "vvvvvvvvvvvvvvvvvv\n",
            "NOT FOUND: $_",
            "^^^^^^^^^^^^^^^^^^\n";
        }
    }



John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to