Strings, pattern matching

Discussion in 'Perl Misc' started by MJS, Sep 28, 2003.

  1. MJS

    MJS Guest

    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;
    ==============================
    MJS, Sep 28, 2003
    #1
    1. Advertising

  2. (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;
    David K. Wall, Sep 28, 2003
    #2
    1. Advertising

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



    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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 28, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anonym
    Replies:
    1
    Views:
    1,000
    Knute Johnson
    Jan 15, 2009
  2. Karin Lagesen

    matching strings in a large set of strings

    Karin Lagesen, Apr 29, 2010, in forum: Python
    Replies:
    13
    Views:
    444
    Bryan
    May 3, 2010
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    220
    Marc Bissonnette
    Jan 13, 2004
  4. rader
    Replies:
    4
    Views:
    177
    Ernest Lergon
    May 7, 2005
  5. Bobby Chamness
    Replies:
    2
    Views:
    212
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page