Go Back   SitePoint Forums > Forum Index > Program Your Site > Perl, Python and Other Languages
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 5, 2009, 05:17   #1
sandy1028
SitePoint Zealot
 
Join Date: Jun 2008
Posts: 183
search for last tag in a file

How to set a flag and search for the last <p> tag in a file and substitute with <con>
sandy1028 is offline   Reply With Quote
Old Nov 5, 2009, 10:01   #2
PhilipToop
SitePoint Enthusiast
 
Join Date: Jun 2007
Location: North Yorkshire, UK
Posts: 36
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
PhilipToop is offline   Reply With Quote
Old Nov 5, 2009, 11:12   #3
sandy1028
SitePoint Zealot
 
Join Date: Jun 2008
Posts: 183
if input is as below
Code:
__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>
output should be
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>
sandy1028 is offline   Reply With Quote
Old Nov 5, 2009, 12:07   #4
PhilipToop
SitePoint Enthusiast
 
Join Date: Jun 2007
Location: North Yorkshire, UK
Posts: 36
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
PhilipToop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 11:38.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved