How to set a flag and search for the last <p> tag in a file and substitute with <con>
| SitePoint Sponsor |


How to set a flag and search for the last <p> tag in a file and substitute with <con>



If you are using perl and the contents of your file are in the variable $a then you can use the regular expression.
$a =~ s/^(.*)<p>(.*?)$/$1<con>$2/s


if input is as below
output should beCode:__DATA__ <id>000044119</id>> <DD>Friday, October 30, 2009</DD>> <p>THis is param1</p> <p>THis is param2</p> <p>THis is param3</p> </root>
Code:__DATA__ <id>000044119</id>> <DD>Friday, October 30, 2009</DD>> <con><p>THis is param1</p>> <p>THis is param2</p>> <p>THis is param3</p></con> </root>



You seem to be changing </p> to </p>> except for the last </p> and putting a <con> before the first <p> and a </con> after the last </p>.
I would do two substitutes.
$a =~ s/<\/p>/<\/p>>/sg
$a =~ s/^(.*?)(<p>.*<\/p>)>(.*?)$/$1<con>$2<\/con>$3/s
If the </p> to </p>> was not intended then just
$a =~ s/^(.*?)(<p>.*<\/p>)(.*?)$/$1<con>$2<\/con>$3/s
Bookmarks