What an "empty" pattern represents?
I wa开发者_如何学Cs playing with some characters:
Patern Subject Status
'/#*/' "\x00" Match
'/#*/' "" Match
'//' "\x00" Match
'//' "" Match
'//' "whatever" Match
'/\x00/' "whatever" Fail
'/\x00/' "" Fail
I realized that a PHP string does not have a NUL character by Default (\x00)
So what an empty pattern '//'
represents to match all PHP strings?
Cuz if we see the last pattern:
'/\x00/' "" Fail
It shows that an empty pattern is not a NUL character.
I know that is a stupid question. But is that curiosity that make me ask.
A pattern give requirements for a string, if the requirements are met, the string matches. An empty pattern has no requirements, so all strings meet all zero of it requirements.
Further, if you ask where the pattern matches, an empty pattern matches in all possible positions. The length of each match is 0. An example from perl (since you're asking about perl-compatible regexps):
$x = "ab";
$x =~ s//./g; # this is search-replace in perl, replace all occurrences of // with .
say $x;
will print .a.b.
as the pattern matched in all possible positions (before a, between a and b, and after b), but did not replace any characters (because the match length is 0).
PHP strings do not work like C strings. Null characters (\x00
) aren't used for termination, so they can exist like any other character inside a string.
精彩评论