Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How to substitute
<p></p> # For the character <.
If character
'>'
exists at the end of the character <.
As in the data for a href..... <p></p> should not be substituted.

#!/usr/bin/perl while (<DATA>) { if($_ =~ /<.+>(<.+>)(.*)/){ $output = $1.$2; $output =~ s/(<|\\)/<\/p><p>/g; print $output; } } __DATA__ <BODY><![CDATA[<p>Developmentally, 2-year-olds are generally more inte +rested in themselves, while dogs do care how their people feel, and i +nstantly recognize a change in emotion.< "While your dog can't compre +hend that you just received a traffic violation, he can tell that you +'re upset the second you walk through the door," Coren says. "In fact +, dogs can detect some subtle changes which even adults can't," adds +Coren. "We can't smell cancer or predict seizures, as dogs can."< Whe +n I posted this story on my Facebook Fan page recently (<a href="http +://www.new.facebook.com/pages/ Steve-Dale/50057343596?ref=ts">www.new +.f acebook.com/pages/Steve-Dale/50057343596?ref=ts, or simply type St +eve Dale into the Facebook search), I received some interesting respo +nses:< Kelle: "Heck, my Italian Greyhound is smarter than most colleg +e students."< Karen: "Depends on how you define smart.

Replies are listed 'Best First'.
Re: p tag substitution
by GrandFather (Saint) on Oct 15, 2009 at 08:01 UTC

    Do not use regexen to parse markup - life if just too short to bother reinventing that particular wheel. Use a suitable HTML module. See Re: Some portion of the text missing for example.


    True laziness is hard work
      How to check if > exists after the  < and then substitute < with </p><p> For example :
      <a ahref="http://www.prospect.org/cs/articles?articleId=11945"> # Here < should not be replaced with </p><p>
        if ($code =~ /=~/ && $data =~ /<p>/) { print "Woe unto you!\n" }

        True laziness is hard work
Re: p tag substitution
by Marshall (Canon) on Oct 15, 2009 at 10:48 UTC
    I didn't know in the first thread what you wanted to do, "text is missing" was not very informative!

    Parsing this HTML stuff can get complicated and I would agree with GrandFather that an HTML module is best for complex situations.

    But....here is a one regex way of doing it (that seems to be what you want). This is only 5, "non-trivial" lines of code, but it did take me some time to write them. There is of course some stuff that is specific to your application and the example that I had!

    #!/usr/bin/perl -w use strict; use constant DEBUG => 1; #makes lines easier to see on screen while (<DATA>) { if ( my ($title, $body) = ($_ =~ /<TITLE>.*?(<p>.+?)<BODY>.*?(<p>.*)/)[0,1] ) { $title .= "</p>"; $body .= "</p>"; $title =~ s|<[^a/p]|<p>|g; $title =~ s|.<p>|.</p><p>|g; $title =~ s|</p>|</p>\n|g if DEBUG; $body =~ s|<[^a/p]|<p>|g; $body =~ s|.<p>|.</p><p>|g; $body =~ s|</p>|</p>\n|g if DEBUG; print "<TITLE>\n$title\n\n", "<BODY>\n$body\n"; } }
    With DEBUG on, this prints:
    <TITLE> <p>Dogs may not smarter than 6-year-olds, but researchers suggest cani +nes might be on par with 2-year-olds.</p> <p>Psychologist Stanley Coren says, "We do know that dogs understand f +ar more than we credit them with, from about 165 words to 250 words." + Even better than understanding our words, dogs know our hand gesture +s and body postures. Dogs may, in fact, far exceed 2-year-olds when i +t comes to reading emotions.</p> <BODY> <p>Developmentally, 2-year-olds are generally more interested in thems +elves, while dogs do care how their people feel, and instantly recogn +ize a change in emotion.</p> <p>"While your dog can't comprehend that you just received a traffic v +iolation, he can tell that you're upset the second you walk through t +he door," Coren says. "In fact, dogs can detect some subtle changes w +hich even adults can't," adds Coren. "We can't smell cancer or predic +t seizures, as dogs can..</p> <p>When I posted this story on my Facebook Fan page recently (<a href= +"http://www.new.facebook.com/pages/ Steve-Dale/50057343596?ref=ts">ww +w.new.f acebook.com/pages/Steve-Dale/50057343596?ref=ts, or simply ty +pe Steve Dale into the Facebook search), I received some interesting +responses.</p> <p>Kelle: "Heck, my Italian Greyhound is smarter than most college stu +dents..</p> <p>Karen: "Depends on how you define smart.</p>
Re: p tag substitution
by ww (Archbishop) on Oct 15, 2009 at 12:29 UTC

    Since you didn't show us desired output, I suspect your spec if faulty because what you show would not result in reasonable .html.

    So, taking a guess (possibly wrong), if you're looking for this:

    <p>Developmentally, 2-year-olds are generally more interested in thems +elves, while dogs do care how their people feel, and instantly recogn +ize a change in emotion.</p> <p>"While your dog can't comprehend that you just received a traffic v +iolation, he can tell that you're upset the second you walk through t +he door," Coren says. "In fact, dogs can detect some subtle changes w +hich even ad ults can't," adds Coren. "We can't smell cancer or predict seizures, a +s dogs can."</p> <p>When I posted this story on my Facebook Fan page recently (<a href= +"http://www.new.facebook.com/pages/ Steve-Dale/50057343596?ref=ts">ww +w.new.f acebook.com/pages/Steve-Dale/50057343596?ref=ts, or simply ty +pe Steve Dal e into the Facebook search), I received some interesting responses:</p +> <p>Kelle: "Heck, my Italian Greyhound is smarter than most college stu +dents."</p>

    you could do this to the data you supplied (which, IMO, implies that you want to replace only those "<" which are followed directly by a space):

    #!/usr/bin/perl use strict; use warnings; my $output; while (<DATA>) { if ($_ =~ /<.+>(<.+>)(.*)/){ $output = $1.$2; $output =~ s/<!\[CDATA\[(.*)/$1/; # print $output; } print "\n\n"; $output =~ s!(< )!</p>\n\n<p>!g; print $output . '</p>' . "\n"; }

    But, as GrandFather said,

    DON'T DO THIS!
    There are too many corner cases, "gotchas," and other complications to make "rolling your own" profitable.