SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Substitution

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

    Substitution

    Code:
    #!/usr/bin/perl
    my $line;
    while (my $line = <DATA>){
    if($line =~ m/<span><p>(.*)/){
    $line = $1;
    $line =~ s/</<\/p><p>/g;
    print $line;
    }
    #}
    }
    __DATA__
    <span><p>This affected me deeply because < I was being treated for depression then. < felt she was replacing me as mother and grandmother.

    From the above code the output is below.

    Code:
    This affected me deeply because </p><p> I was being treated for depression then. </p><p> felt she was replacing me as mother and grandmother.
    But I have to print the line, such that it does substitution

    Code:
    <span><p>This affected me deeply because </p><p> I was being treated for depression then. </p><p> felt she was replacing me as mother and grandmother.

  2. #2
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to check for every < that doesn't get followed by a closing > before it sees another open <.


    Code Perl:
    #!/usr/bin/perl
    my $line;
    while  (my $line = <DATA>) {
     
        if ($line =~ m/<span><p>(.*)/) {
            $line =~ s/<(?![^<]*?>)/<\/p><p>\1/g;
            print $line;
        }
    }
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User


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
  •