Strings, pattern matching

M

MJS

I have a hard time matching and replacing this pattern. It works fine
for single line but when I want to replace multiple line pattern, it
doesn't seem to work. I spend a lot of time on faq but I guess I m not
smart enough to figure out where I am going wrong. I couldn't do it
using the regular exp. so I tried doing it using Tie::File.

Please help me with this.

============================
use Tie::File;
use strict;
use warnings;

# tie @array to filename using Tie::File
tie my @array, 'Tie::File', 'result.txt' or die "Cannot open
result.txt:$!";

while(<result.txt>){

for(@array) {

my $pattern = "The text

to be

searched

in differnt

lines inclunding white lines and indentation";
my $replace = " The replacing
text
also in
multiple lines
including indentation and white lines ";

s/$pattern/$replace/;

}
}
untie @array;
==============================
 
D

David K. Wall

I have a hard time matching and replacing this pattern. It works fine
for single line but when I want to replace multiple line pattern, it
doesn't seem to work. I spend a lot of time on faq but I guess I m not
smart enough to figure out where I am going wrong. I couldn't do it
using the regular exp. so I tried doing it using Tie::File.

Tie::File lets you access the file as an array, where each element of the
array is one line, so it's probably not the right choice for a multi-line
search and replace.

If the file is small, you might try slurping it into a scalar:

open IN, 'infile.txt' or die "Error opening infile.txt: $!";
local $/ = undef;
$_ = <IN>;
close IN;

# see note below about metacharacters
s/$pattern/$replace/s; # see perlop for the s option on s///

open OUT, '>', 'outfile.txt' or die "Error opening outfile.txt: $!";
print OUT $_;
close OUT;

You don't have to use two files, but it's safer.
Please help me with this.

============================
use Tie::File;
use strict;
use warnings;

# tie @array to filename using Tie::File
tie my @array, 'Tie::File', 'result.txt' or die "Cannot open
result.txt:$!";

while(<result.txt>){

Two problems here. You can't just put the filename inside <> and read the
file. <> works on filehandles, so you have to open() the file first. But
since you've already tied the file to @array, that's the only thing you
need to loop over.
for(@array) {

my $pattern = "The text

to be

searched

in differnt

lines inclunding white lines and indentation";
my $replace = " The replacing
text
also in
multiple lines
including indentation and white lines ";

s/$pattern/$replace/;

s/$pattern/$replace/s;

You should also check your pattern for regex metacharacters, or use
quotemeta() or \Q. For example:

s/\Q$pattern/$replace/s;
 
T

Tad McClellan

MJS said:
I have a hard time matching and replacing this pattern. It works fine
for single line but when I want to replace multiple line pattern, it
doesn't seem to work.


You must arrange to _have_ a multiline string if you want a
multiline pattern to match the string.

I spend a lot of time on faq


Which FAQ?

Hopefully it was this one:

I'm having trouble matching over more than one line. What's wrong?

but I guess I m not
smart enough to figure out where I am going wrong. I couldn't do it
using the regular exp. so I tried doing it using Tie::File.


Tie::File is not an alternative to regexes.

Please help me with this.


The first sentence of the FAQ answer points out your problem:

Either you don't have more than one line in the string you're looking
at (probably)
...
There are many ways to get multiline data into a string
...

tie my @array, 'Tie::File', 'result.txt' or die "Cannot open


Tie::File puts *one* line per array element.

while(<result.txt>){


The <input> operator puts *one* line into $_.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top