Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

The simplest perl Multi Line matching.

by dannyd (Sexton)
on Feb 08, 2011 at 07:52 UTC ( #886899=perlquestion: print w/ replies, xml ) Need Help??
dannyd has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I have been trying to match the pattern below, but have been greatly unsuccessful. There are 5 lines in the Output

-------------------------------------------------------------- Perl Progress -------------------------------------------------------------- dannyd@cli>
This is the code I have been trying,
#!/usr/bin/perl open (FH, "test.txt"); while (<FH>) { print "Match!!\n" if ($_ =~ /^-+\nBG/ms); } close (FH);

I am able to match any one of the lines, but when I try to match the next one using the same regular expression, It fails, please help me figure out why this is happening

And any good material to read on multi line matching would also be highly appreciated

Comment on The simplest perl Multi Line matching.
Select or Download Code
Re: The simplest perl Multi Line matching.
by Anonymous Monk on Feb 08, 2011 at 08:03 UTC

      Ok Thanks, that was very cunning, I just thought about that after reading your post.

      I'm going through the links you sent.

      Ive modified it to this, seems to work fine, please tell me if you have anything else to say about the code.

      #!/usr/bin/perl my $newstr = ''; open (FH, "test.txt") || die "Could not open file.\n"; while(<FH>) { $newstr = $newstr.$_; print "!!\n" if ($newstr =~ /^-+\nPerl Progress\n-+\n\s\ndannyd\@c +li>\s$/m); } close (FH);

      Thanks a lot by the way, in my opinion this got solved super fast!!

        my $newstr = '';
        Altough it is not "wrong" there is no reason at all to initialize a lexical variable to the empty string (or zero) as the my takes care of creating a brand new "empty" variable with the value of undef, empty string or zero (depending on how you look at it).

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: The simplest perl Multi Line matching.
by elwarren (Curate) on Feb 08, 2011 at 15:45 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://886899]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2014-09-18 13:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (115 votes), past polls