This is a misnomer. There isn't a "null character" between every character.

There couldn't be, else there would need to be a null character--usually defined as chr(0) or "\x00" or "\0" etc.--between each null character and the adjacent non-null character. Then there would need to be a null character between each pair of adjacent null characters... As you can see that is recursive and every string would be of infinite length;)

Possibly a better way of stating it is that the null-length string--defined as a string of length 0 (zero) bytes--is deemed to exist between each pair of characters in a string. Also before the first character and after the last character.

The regex s// /g will 'find the null-length strings between each character' and 'replace them' with spaces.

$s= 'ABCDEFG' $s =~ s// /g print "'$s'" ' A B C D E F G '

Viewed this way, ie. as a null-length string, then it makes a certain amount of sense. If you remove the first character and the last character from a two character string, the what you have left is a null-length string. Therefore, it could be viewed that a null-length string exists between the first and last characters of the two character string. It's easy to see that you can extend this logic to say that it exists before and after every character in the string.

Whether this 'deemed to exist'ness of the null-length string has any useful properties is an open question. You can do thing like:

$s= 'ABCDEFG' $s =~ s/^/prefix-/ print $s prefix-ABCDEFG $s =~ s/$/-postfix/ print $s prefix-ABCDEFG-postfix

but it's easy to see that either of these is easily achieved by concatenation.

I've just racked my brains--for at least 5 minutes:)--to find an example where matching for the null-length string is useful, but so far I've failed. Conversely, I have encountered many occasions when I've had to go to extra and sometimes extreme lengths to prevent a regex matching the null string in order to achieve my aims. I often wish that if I construct a regex that leads the engine to conclude that I want to match a null string, that it would issue a warning, and (optionally?) die.

Maybe someone else will offer an example of where matching a null-length string is useful.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

In reply to Re: Re3: replacing pipe "|" by BrowserUk
in thread replacing pipe "|" by nadadogg

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.