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.
|