CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Match text on multiple lines.

 



rst
Deleted

Oct 16, 2000, 11:13 PM

Post #1 of 2 (1564 views)
Match text on multiple lines. Can't Post

I have just started programming Perl I realise that I so I definitely a beginner, but I am hooked.

I have worked with this problem for a couple of weeks, I think I have located the problem but I cant seem to find the solution.

What I want to do is to search trough a large INI like file, in this file there are a section called [Macro] in this section there are reference to a disk. In this case the drive letter is P:, I want to put this into a variable.

Everything works fine if I feed one by one line. But then there are multiple instances in the file that matches. Like I said the problem seems to bee that I try to match the text in an array. But I am not sure. Can anyone please help me, if noting else it would be nice to get some pointers to what I am doing wrong.

Samples of the files

The script:

#!/usr/local/bin/perl
# A utility to replace substrings in all ZEN prodused axt files
# copyright 200, Robert Strand
#
# Usage: replacer.pl file
#
if (($ARGV[0] eq "") | | ($ARGV[0] eq "?")) {
print "A utility to making dynamic axt files\n";
end;
} else {
$file = $ARGV[0];

open (FILE, $file);
@thisfile = <FILE>;
close (FILE);


@thisfile =~ m/\[Macro.*Value=(.):.*$/ism;
$source_disk = $1;

The file:
Lots of more text…………………..
[Application Name]
Value=TESTSW

[Macro]
Name=SOURCE_PATH
Value=P:\TESTFOLDER1\ TESTFOLDER2\

[Application Caption]
Value=TESTNAME

[Registry Value Create]
Type=String
Flag=Write Always
Key=HKEY_CURRENT_USER\Software\Microsoft\Office\9.0\Common\LanguageResources
Name=1044
Value=On
Lots of more text…………………..


------------------
Best Regards
Robert Strand, rst@bdc.no


japhy
Enthusiast / Moderator

Oct 21, 2000, 8:55 AM

Post #2 of 2 (1564 views)
Re: Match text on multiple lines. [In reply to] Can't Post

You can't do a pattern match on an array. Do it line-by-line:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open FILE, $file or die "can't read $file: $!";
$/ = "";
# this allows you to read in chunks at a time, where a chunk is data between 2 or more newlines
while (<FILE> ) {
last if /^\[Macro\].*Value=(.):/s;
}
close FILE;
$drive = $1;
</pre><HR></BLOCKQUOTE>

You need the 's' modifier at the end of the regex to allow the '.' to match ALL characters (usually, it won't match \n).

Read up on perldoc perlre, or even perldoc perlretut if you need a tutorial.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives