match pattern of multiple lines

Discussion in 'Perl' started by John Black, Jul 15, 2004.

  1. John Black

    John Black Guest

    Hi,
    I need to match the following pattern with a file,

    begin <some string>
    <some thing this line>
    <some thing this line>
    ......
    end

    I check the web, it says I should use pattern modifier s & m, so I
    write it this way,

    while (<MyFile>){
    if (/^(\s*)begin(.*)^(\s*)end/smi){
    ... call my function...
    }
    }

    But it never matches the pattern, do you see the problem?

    Thanks!
     
    John Black, Jul 15, 2004
    #1
    1. Advertising

  2. John Black wrote:
    >
    > while (<MyFile>){
    > if (/^(\s*)begin(.*)^(\s*)end/smi){
    > ... call my function...
    > }
    > }
    >
    > But it never matches the pattern, do you see the problem?


    If you haven't set the $/ variable, you are only testing one line at a
    time. One solution is to slurp the file instead of using a while loop:

    local $_ = do { local $/; <MyFile> };

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 16, 2004
    #2
    1. Advertising

  3. On Thu, 15 Jul 2004 16:26:28 -0600, John Black wrote:

    >
    > Hi,
    > I need to match the following pattern with a file,
    >
    > begin <some string>
    > <some thing this line>
    > <some thing this line>
    > ......
    > end
    >
    > I check the web, it says I should use pattern modifier s & m, so I
    > write it this way,
    >
    > while (<MyFile>){
    > if (/^(\s*)begin(.*)^(\s*)end/smi){
    > ... call my function...
    > }
    > }
    >
    > But it never matches the pattern, do you see the problem?
    >
    > Thanks!


    Hi,

    This won't work because you are reading the file one line at a time. You
    may want to read the whole file at one time by using this:

    local $/; # slurp mode ( to read the file into a string )
    my $content = <MyFile>;

    if ( $content =~ /^\s*begin\s+(.+?)\s+end$/smi ) { CallFunc(); }

    Also, check the regular expression you are using as it doesn't seem
    to be right. I've modified it in the example above but I'm not sure if it
    is what you want.

    Note that reading the file at one time can be memory-intensive if the file
    is too big.

    good luck,
     
    Mohammad Mahmoud Khajah, Jul 16, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terminator
    Replies:
    9
    Views:
    269
    Ross Ridge
    Jun 24, 2009
  2. Markus Fischer
    Replies:
    9
    Views:
    166
    7stud --
    Apr 8, 2011
  3. Chris L.

    Multiple Line Pattern Match

    Chris L., Apr 9, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    126
    Tad McClellan
    Apr 10, 2006
  4. samuel

    Multiple Line Pattern Match problem

    samuel, May 31, 2007, in forum: Perl Misc
    Replies:
    7
    Views:
    139
    samuel
    Jun 4, 2007
  5. Cah Sableng
    Replies:
    0
    Views:
    242
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page