Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Strings, pattern matching

Reply
Thread Tools

Strings, pattern matching

 
 
MJS
Guest
Posts: n/a
 
      09-28-2003
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;
==============================
 
Reply With Quote
 
 
 
 
David K. Wall
Guest
Posts: n/a
 
      09-28-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (MJS) wrote:

> 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;


>
> }
> }
> untie @array;

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-28-2003
MJS <(E-Mail Removed)> wrote:

> 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.

Tie::File is an alternative to <input>.


> 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 $_.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
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
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Matching neighbouring words of a pattern using Regex CV Perl 2 08-31-2004 12:27 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM
Pattern matching help! grep emails from file! danpres2k Perl 3 08-25-2003 02:47 PM
A newbie question on pattern matching DelphiDude Perl 3 07-26-2003 12:54 PM



Advertisments