Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > match pattern of multiple lines

Reply
Thread Tools

match pattern of multiple lines

 
 
John Black
Guest
Posts: n/a
 
      07-15-2004

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!

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-15-2004
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
 
Reply With Quote
 
 
 
 
Mohammad Mahmoud Khajah
Guest
Posts: n/a
 
      07-15-2004
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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Match a pattern multiple times, returning matches, captures andoffset? Markus Fischer Ruby 9 04-08-2011 07:53 PM
What is the best method to match a pattern in set of lines Terminator Python 9 06-24-2009 05:17 PM
Multiple Line Pattern Match problem samuel Perl Misc 7 06-04-2007 02:12 AM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
Multiple Line Pattern Match Chris L. Perl Misc 5 04-10-2006 12:08 PM



Advertisments