Hi
I am parsing a pattern '<default>any text</default>' pattern in a file and wherever found, I want to replace that by '<default>enabled</default>'.
Now the problem is the above mentioned pattern may occur in a single line and in multiple line as well. So the input file may look something like

******* inputfile****************
this is line 1
this is line 2
<default>sometext</default>
this is line 3
<default>some more text
some more in next line
</default>
this is last line
*************************************
I am trying with this, which is working when the pattern is in single line but not for multiple line. The code for multiple line tried is commented out.

use strict; use warnings; use Tie::File; my $file = "inputfile"; my @array; tie @array , 'Tie::File', $file ; my $start = '<default>'; my $end = '</default>'; my $val = 'enabled'; my $replace = $start.$val.$end; print "replace $replace \n"; for (my $i = 0 ; $i <= $#array ; $i++) { #below if block is tried for multiple line pattern but not working # if ($array[$i] =~ /^<default>/../<^\/default>/) if($array[$i] =~ /^<default>/) #this is working { $array[$i] =~ s/^<default>.*<\/default>/$replace/g; } } untie @array;

seeking help on how can it be made to match both the patterns ?


In reply to parsing a multi-line pattern and replacing by ghosh123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.