yangtse has asked for the wisdom of the Perl Monks concerning the following question:

Hi Forks, Here is my example,
$line = "this is a test [[ this is a test ] ]";
I am going to remove [[ this is a test ]] from the $line? I am looking for how to remove the pattern [[.*]] from multiple lines my approach  $line =~ s/\[\[[^\]]*\]\s*\]/ /msg; but it is not working. Can someone have any idea here? Thanks in advance!

Added code tags and [ entities - dvergin 2002-04-24

Replies are listed 'Best First'.
Re: substition in multiple lines
by thelenm (Vicar) on Apr 24, 2002 at 17:36 UTC
    This probably doesn't help, but your regex works for me when I run it. Can you give more details as to how it's failing for you?
    $line = "this is a test [[ this is a test ] ]"; $line =~ s/\[\[[^\]]*\]\s*\]/***FOO***/sg; print "Line: '$line'\n";
    prints
    Line: 'this is a test ***FOO***'
    You also don't need the /m modifier in this case, since you're not using ^ or $ in your regex.
Re: substition in multiple lines
by emilford (Friar) on Apr 24, 2002 at 17:44 UTC
    I agree with thelenm that your code seems to work just fine. I'm trying to figure out what you think it is doing wrong.
    Input File -------------------------- this is a test [[ this is a test ]] this is also a test [[ this is also a test ]] Output -------------------------- this is a test this is also a test Code -------------------------- #!/usr/bin/perl -w use strict; open(FILE, "<test.txt") || die "Error: $!"; while(<FILE>) { $_ =~ s/\[\[[^\]]*\]s*\]/ /msg; print; } close(FILE);
    Seems to be working to me! -Eric
Re: substition in multiple lines
by joealba (Hermit) on Apr 24, 2002 at 17:39 UTC
    Hmm.. it works for me.

    I'd say you should just make your regexp a little more robust:

    $line =~ s/(\[\s*){2}[^\]]*(\]\s*){2}/ /sg;

    Remember that the 's' regexp modifier says to treat the entire string as if it were a single line. 'm' alters the way the regexp treats start/end of lines. For this example, you only need the 's' modifier.