Is there an explanation, why split() behaves like this:

Yes, because its documented that split behaves that way, split splits strings apart into pieces (split cuts), even if there is nothing in between

Read perldoc -f split and consider this

use Data::Dump qw/ dd /; dd( split /\D/, q/12Q34/ ); dd( split /\D/, q/12ab34/ ); dd( split /(\D)/, q/12ab34/ ); __END__ (12, 34) (12, "", 34) (12, "a", "", "b", 34)

Q is not a digit between 12 and 34
empty string "" is not a digit between a and b
empty string "" is not a digit between a and b (a and b are preserved not discarded

split cuts a string apart, discarding the cut pieces unless you (keep) them


In reply to Re: Empty strings after split /(\W)/ (as documents as split cuts) by Anonymous Monk
in thread Empty strings after split /(\W)/ by wollmers

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.