Regarding the code that you tried, it looks like it will place "<keyword>" and "</keyword>" around the separators (commas, etc) as well as around the keywords themselves; looks like you need to study "perldoc perlre" a little more regarding the "zero-width positive look-ahead assertion", because you're using it here in a way that doesn't help your task.

You want "split" to return all the original input characters, and just put "keyword" tags immediately around each string which does not consist of separator characters. So add some logic to the "map" block, like this:

my $input = "kw1,kw2; kw3 &mdash; kw4&hyphen;kw5"; # separator is any string consisting of comma, semicolon, # &mdash;, &ndash; or &hyphen;, bounded by 0 or more whitespace: my $sep = qr{ \s* (?: , | ; | \&(?:[mn]dash|hyphen); ) \s* }x; # in the map block, add keyword tags to non-separator items my @out = map { /$sep/ ? $_ : "<keyword>$_</keyword>" } split /($sep)/ +, $input; print join "\n",@out,"";
In this case, whitespace alone will not trigger a split; a single keyword item could contain multiple words separated by whitespace.

In reply to Re: split keywords by graff
in thread split keywords by Anonymous Monk

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.