I do indeed admire johngg 's regex approach (and have ++ed it), but at the same time, hesitate to walk away without pointing out that it has NO capacity to flag mis-nesting (mis-nesting by .html or .xml standards, that is) and suspect that at some point bwgoudey's input data may have an anomaly or two.

Suppose the $html in johngg's Re: Split tags and words nicely were changed to:

q{<tag ref=1><tag ref=1a>Start<tag ref=2>and </tag><tag "ref=3">more</ +tag>and end};
Note unbalanced opens (4) and closes (2)

Leaving all else alone, output becomes:

<tag ref=1> <tag ref=1a> Start <tag ref=2> and </tag> <tag "ref=3"> more </tag> and end

... which offers no ready hint or markup or warning that the tags were mis-nested.

This is part of the reason that so many monks will advise against trying to parse the likes of .html or .xml with regexen and advocate the use of some of the modules mentioned above.


In reply to Re^2: Split tags and words nicely by ww
in thread Split tags and words nicely by bwgoudey

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.