in reply to Re: search and replace
in thread search and replace

<p>This is param1</p> <p>This is second para</p> <p>this is param3</p>
The output should be
<con><p>This is param1</p> <p>This is second para</p> <p>this is param3</p></con>
Please tell me how to get the output this way

Replies are listed 'Best First'.
Re^3: search and replace
by Your Mother (Archbishop) on Nov 05, 2009 at 05:16 UTC
    printf("<con>%s</con>", do { local $/; <DATA> } ); __DATA__ <p>This is param1</p> <p>This is second para</p> <p>this is param3</p>

    Facetiousness aside, the DATA iteration isn't working the way you think/want and though there are plenty of tricks to get it to do what you're after in this context it's not a good practice; you should parse/edit XML with an XML parser/writer. XML::Twig and XML::LibXML are (generally) the favorites around these parts.

      __DATA__ <root> <p>This is param1</p> <p>This is second para</p> <p>this is param3</p> </root>
      How to search for the occurance of first <p> tag in a file and replace it with <con><p> and search for the last closing </p> tag and replace it with </p></con>
        #!/usr/bin/perl use strict; use warnings; my $flag=0; while(<DATA>){ if($flag == 0){ if($_ =~ m/<p>/){ $_ =~ s/<p>/<con>/; $flag=1; } } print $_; } __DATA__ <root> <p>This is para1</p> <p>This is para2</p> </root>
        The output should be
        <root <con> <p>This is para1</p> <p>This is para2</p> </con> </root>
        Only after the last p tag in the file I have to add </con>