Hi Aleena,

No idea if this is cookie worthy, but you got me interested in looking at the Text::Balanced module:

use Text::Balanced qw(extract_multiple gen_extract_tagged); my $codes = qr(A|ABBR|ACRONYM|B|BIG|CITE|CODE|DFN|EM|I|KBD|SAMP|SMALL| +SPAN|STRONG|SUB|SUP|TT|VAR); my $extractor = [ gen_extract_tagged('<!--', '-->', ''), gen_extract_tagged("$codes<", '>', '') ]; # Join all plain text segments, and element substitutions; removing co +mments sub inline { my $text = shift; my $result = ''; for (extract_multiple $text, $extractor) { $_ = element(lc $1,$2) if /^($codes)<(.*)>$/; $result .= $_ unless (/^<!--/); } return $result; } # Handle a tag element with attribute if one exists, and then recurse +into it sub element { my ($id,$inner) = @_; return ($inner =~ s/\|([^<>]+?)$// ? "<$id $1>" : "<$id>") . inline($inner) . "</$id>"; } print inline 'Anyone who watches the Syfy channel knows that on Monday nights ' +. 'they aired three television series I<A<EurSUP<e>ka|href=' . '"Movies_by_series.pl?series=EWA#EUReKA">|class="title">, I<A<Wareh +ouse ' . '13|href="Movies_by_series.pl?series=EWA#Warehouse_13">>, and I<A<A +lphas' . '|href="Movies_by_series.pl?series=EWA#Alphas">>. Some might not be + aware ' . 'that these three series have formed a crossover cosmology which I +call A<' . 'EWA|href="Movies_by_series.pl?series=EWA"><!-- This is a long stri +ng. -->';

Which prints the same output as in your example using Perl 5.8.8. The key is using extract_tagged which will match your codes exactly. For instance this is the results of the first extract_multiple :

'Anyone who watches the Syfy channel knows that on Monday nights they +aired three television series ' 'I<A<EurSUP<e>ka|href="Movies_by_series.pl?series=EWA#EUReKA">|class=" +title">' ', ' 'I<A<Warehouse 13|href="Movies_by_series.pl?series=EWA#Warehouse_13">> +' ', and ' 'I<A<Alphas|href="Movies_by_series.pl?series=EWA#Alphas">>' '. Some might not be aware that these three series have formed a cross +over cosmology which I call ' 'A<EWA|href="Movies_by_series.pl?series=EWA">' '<!-- This is a long string. -->'

Everything is already broken up nicely and all that is left to do is recurse inside the tags found to handle any nested codes.

This does nothing more than your code however, as usual just TIMTOWTDI.


In reply to Re: RFC: Is there a better way to use Text::Balanced? by Loops
in thread RFC: Is there a better way to use Text::Balanced? by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.