SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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>

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2007
    Location
    North Yorkshire, UK
    Posts
    483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •