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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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



Oct 16, 2000, 11:13 PM

Post #1 of 2 (4535 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:

# A utility to replace substrings in all ZEN prodused axt files
# copyright 200, Robert Strand
# Usage: file
if (($ARGV[0] eq "") | | ($ARGV[0] eq "?")) {
print "A utility to making dynamic axt files\n";
} 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]


[Application Caption]

[Registry Value Create]
Flag=Write Always
Lots of more text…………………..

Best Regards
Robert Strand,

Enthusiast / Moderator

Oct 21, 2000, 8:55 AM

Post #2 of 2 (4535 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;

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