Multi-line pattern matching 
Author Message
 Multi-line pattern matching

I'm using Perl4.046 on DOS ('cos my boss wants the util to work on DOS:-(

Here's the perloblem...
I'm trying to search and replace patterns in a file...but I'm finding problems
trying to find patterns that have a "newline" embedded i.e. they're on more
than one line.  The regexps are taken from an associative array...

Look at this code
--------------------------
$* = 1;      #enable multi-line pattern matches
$^I = ".old";                 #make "<sourcefile>.old"

while (<>) {            #read the source file and do replacements
   foreach $from (keys(%table)) {
       $to = $table{$from};
       eval "s/$from/$to/g;";
   }
  print;

Quote:
}

---------------------------

This works very well, if I'm looking for patterns that are on just one
line...since the while loop is reading in one line at a time, the reg exp
is only executed on that line.   If the pattern I'm looking for happens
to include a newline anywhere, I will miss the pattern.

I've tried putting the "foreach" loop outside the "while (<>)", but I
need to be able to "seek" back to start of the file for the new
pattern(s) extracted from the associative array.  The seek doesn't seem
to work:-(  Please help.

Or better still, is there a way I can read the whole file into a string
and then eval the regexp on that string as many times I need to
(according to the size of the array containing the $from and $to patterns).

I've been trying this for over a week now...I'm desperate:-(

thanks in advance,

Ajay Busari




Sun, 22 Jun 1997 23:36:24 GMT  
 Multi-line pattern matching

Quote:

> I'm trying to search and replace patterns in a file...but I'm > finding

problems trying to find patterns that have a "newline" > embedded.

You've enabled multi-line pattern *matches*, but not multi-line *reads*.
Since your source file is being read a line at a time, you never get
"abcdef\nghi" in $_ to match against a pattern.

If you put this line in at the start of the script:

$/='' ;

then perl will read entire paragraphs (ended by a blank line, ie "\n\n")
and you can match matterns with embedded newlines, unless they contain
consecutive embedded newlines.

Quote:
> but I need to be able to "seek" back to start of the file for

seek() doesn't work properly in Perl 4.036 M03. It's a bug.

Quote:
> Or better still, is there a way I can read the whole file into a

string > and then eval the regexp on that string as many times I need
to > (according to the size of the array containing the $from and $to

Quote:
> patterns).

Yes, try this:

undef $/ ;              # No line terminator, allowing
$_ = <> ;               # Read whole file into one string

foreach $from (keys(%table)) {
       $to = $table{$from};
       eval "s/$from/$to/g;";

Quote:
}

print;

This will work provided you don't run out of memory...


enjoying it. ftp://ftp.ee.umanitoba.ca/pub/msdos/perl



Tue, 24 Jun 1997 05:40:23 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Multi line pattern matches?

2. Multi-line pattern matching in 5.001n

3. Multiline pattern matching with command line invocation

4. Multi line pattern match

5. Multi-line pattern matching?

6. bug in anchored, multiline pattern match

7. Pattern bug matching whitespace in multi-line match?

8. pattern matching in multi-line strings fails under perl4.034

9. regex to match a multi line pattern

10. Can't Match Multi-Line Pattern

11. regex to match a multi line pattern

12. multiline, multi pattern match

 

 
Powered by phpBB® Forum Software