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

I am trying to make replacement for the following using pattern matching ie. s###g;
- Look for pattern F1
- Start new line and replace with F2 content
(This involves special characters ie. *)
[F1] //*********************************** //* Filename: Config1.c //* Comment: [F1] is replaced by [F2] //* //*********************************** [F2] /***********************************/ /* Filename: Config2.c */ /* Comment: [F2] replace [F2] */ /* */ /* */ /***********************************/

How can i achieve in the simplest way?
1. Define 2 paragraph strings $F1 & $F2
2. Detect pattern /[F1]/ and start to copy from next line Thanks.

Replies are listed 'Best First'.
Re: Multiple lines replacement using s###g;
by ikegami (Patriarch) on Apr 06, 2010 at 06:15 UTC

    To create a pattern that matches a string literally, use quotemeta, also available as \Q..\E.

    my $f1 = <<'__EOI__'; //*********************************** //* Filename: Config1.c //* Comment: [F1] is replaced by [F2] //* //*********************************** __EOI__ my $f2 = <<'__EOI__'; /***********************************/ /* Filename: Config2.c */ /* Comment: [F2] replace [F2] */ /* */ /* */ /***********************************/ __EOI__ s/\Q$f1/$f2/g;
Re: Multiple lines replacement using s###g;
by biohisham (Priest) on Apr 06, 2010 at 07:29 UTC
    You seem to want an output similar to
    [F1] //*************************************/ //* Filename: Config2.c */ //* Comment: [F2] replace [F2] */ //* */ //*************************************/ [F2] //***********************************/ //* Filename: Config2.c */ //* Comment: [F2] replace [F2] */ //* */ //* */ //***********************************/
    where comments would be cleaned up as well and not some swapping of entire records right ? if so the case then consider:
    use strict; use warnings; local $/=''; my %hash; while(my $data=<DATA>){ my @array = split(/\n/,$data,2); $hash{$array[0]}=$array[1]; } foreach my $key (keys %hash){ if ($key =~ /\[F1\]/){ $hash{$key} = $hash{'[F2]'} ; } print $key, "\n", $hash{$key},"\n"; } #use Data::Dumper; #print Dumper(\%hash); __DATA__ [F1] //*********************************** //* Filename: Config1.c //* Comment: [F1] is replaced by [F2] //* //*********************************** [F2] /***********************************/ /* Filename: Config2.c */ /* Comment: [F2] replace [F2] */ /* */ /* */ /***********************************/


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.