正则表达式匹配反斜杠需要使用四个反斜杠
方法如下:
$subject='\\';
var_dump(preg_match('/^\\\\$/', $subject));
?>
/^\\\$/ /^\\\\$/ 这两个正则表达式 三个或者是四个反斜杠都是匹配的,但是PHP官方文档明确指出需要使用四个反斜杠。
Although 3 slashes may appear to work (don’t ask me why!), the correct way in PHP is definitely to use 4 slashes:
http://www.php.net/manual/en/regexp.reference.escape.php
Note:
Single and double quoted PHP strings have special meaning of backslash. Thus if \ has to be matched with a regular expression \\, then "\\\\" or '\\\\' must be used in PHP code.
参考资料