SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Add p tag

Hybrid View

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

    Add p tag

    Code:
    #!/usr/bin/perl 
    while(<DATA>){
     s/(.*)<$/<p>$1<\/p>/g;
     print $_; 
    
    }
     __DATA__ 
    9912290449 
    *In, a law prohibiting in United
    States went into effect.< 
    In, a law prohibiting in United
    States went into effect.<
    9912290450

    Suppose the line is divided into two lines. How to add the start <p> tag at the start of the line. Now the ouptput is

    9912290449 *In, a law prohibiting in United States <p>went into effect.</p> In, a law prohibiting in United States <p>went into effect.</p> 9912290450


    How to add the <p> tag. Where the output should look like

    Code:
    9912290449 <p>*In, a law prohibiting in United States went into effect.</p> <p>In, a law prohibiting in United States went into effect.</p> 9912290450

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unless you can define exactly what a "line" is there is no way to help you except to write code that is contrived to do what you are asking only for the lines of data you posted but that will not work for other lines of data.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KevinR View Post
    Unless you can define exactly what a "line" is there is no way to help you except to write code that is contrived to do what you are asking only for the lines of data you posted but that will not work for other lines of data.
    Code:
    #!/usr/bin/perl
    while(<DATA>){
      if (/^(\[\w+\])/) {
        $f = $1;
      }
      elsif ($f =~ m/\[NEW\]/) {
            #$_ =~ s/\[NEW\]/\[NEW\]<p>/;
    #        $_ =~ s/\[NEW\](.*){\w*}/<p>$1<\/p>/g;
            $_=~ s/<$/<\/p><p>/g;
       # s/(\[NEW\].+?)\<?(\n\[)/$1<p>$2/xsg;
    
            print $_;
    }
    
    }
    
    __DATA__
    [NEW]
     Dear Carolyn: My husband and I divorced after 42 years. It didn't take long
    for him to find a replacement, but I was happy for him at the time.<
    them and let my grandchildren dig through her purse as if she were their
    grandmother.<
    [OLD]
    I have to add '<p> tag after [NEW]. and closing '</p> tag before [OLD].
    I have to replace '<' with '</p><p>'.
    But before [OLD] I have to not add '<p>' tag it should be only '</p>'.

    For the given input
    output should be


    Code:
    [NEW]<p>
     Dear Carolyn: My husband and I divorced after 42 years. It didn't take long
    for him to find a replacement, but I was happy for him at the time.</p><p>
    them and let my grandchildren dig through her purse as if she were their
    grandmother.</p>
    [OLD]

  4. #4
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This might be contrived to work with your example data because that's all the data I can see so I have to make assumptions:

    Code:
    #!/usr/bin/perl
    while(<DATA>){
       s/\Q[NEW]/[NEW]<p>/;
       s/\Q[OLD]/<\/p>[OLD]/;
       s#\.<#</p><p>#;
       print;
    }
    __DATA__
    [NEW]
     Dear Carolyn: My husband and I divorced after 42 years. It didn't take long
    for him to find a replacement, but I was happy for him at the time.<
    them and let my grandchildren dig through her purse as if she were their
    grandmother.<
    [OLD]
    The biggest assumption is the < is always preceded by a dot and only occurs once perl line. If its always once and always on the end of a line you could add the end of line anchor to make more efficient.

    Code:
       s#\.<$#</p><p>#;
    if not and it can be more than once perl line add the "g" modifier:

    Code:
       s#\.<#</p><p>#g;
    if "<" is not always preceded by a dot just remove it from the regexp.

  5. #5
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this :

    once perl line

    should be:

    once per line


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
  •