in reply to Set the flag

$flag1 = 1; $flag2 = 0; $flag3 = 0; s{<p>}{<con>}; s{.*\K</p>}{</con>}s; # Faster, Perl >= 5.10 s{(.*)</p>}{$1</con>}s; # Slower, any Perl

Replies are listed 'Best First'.
Re^2: Set the flag
by Anonymous Monk on Nov 06, 2009 at 01:01 UTC
    The above code is replacing every <p> with <con>.
    I have to replace only in the first <p> and skip replacing of <con> for other <p> tag.
    The replacement should work only for the first <p> tag in a file and I have to replace only in the last </p> and skip replacing of </con> for other </p> tag.
    The replacement should work only for the last </p> tag in a file Please help me on this.

      The above code is replacing every <p> with <con>.

      No, just the first. You'd need to add /g to make it replace every instance.

      You said you needed to search the file. You aren't searching every line, right?