且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在单引号和双引号PHP正则表达式模式中正确地转义反斜杠以匹配文字反斜杠

更新时间:2022-11-14 20:59:17

PHP的解析器和正则表达式引擎(PCRE)都将反斜杠字符(\)视为转义字符.如果编写单个反斜杠字符,则PHP解析器会将其视为转义字符.如果编写两个反斜杠,则PHP的解析器会将其解释为文字反斜杠.但是,当在正则表达式中使用时,正则表达式引擎会将其用作转义字符.为了避免这种情况,您需要根据引用模式的方式编写四个反斜杠字符.

A backslash character (\) is considered to be an escape character by both PHP's parser and the regular expression engine (PCRE). If you write a single backslash character, it will be considered as an escape character by PHP parser. If you write two backslashes, it will be interpreted as a literal backslash by PHP's parser. But when used in a regular expression, the regular expression engine picks it up as an escape character. To avoid this, you need to write four backslash characters, depending upon how you quote the pattern.

要了解两种类型的引用模式之间的区别,请考虑以下两个var_dump()语句:

To understand the difference between the two types of quoting patterns, consider the following two var_dump() statements:

var_dump('~\\\~');
var_dump("~\\\\~");

输出:

string(4) "~\\~"
string(4) "~\\~"

在单引号中使用转义序列\~在PHP中没有特殊含义.三个反斜杠也可以工作,因为PHP解析器不知道转义序列\~.因此\\将变为\,但\~将保留为\~.

The escape sequence \~ has no special meaning in PHP when it's used in a single-quoted string. Three backslashes do also work because the PHP parser doesn't know about the escape sequence \~. So \\ will become \ but \~ will remain as \~.

您应该使用哪个:

为清楚起见,当我要匹配文字反斜杠时,我将始终使用~\\\\~.另一个也可以,但是我认为~\\\\~更清楚.

For clarity, I'd always use ~\\\\~ when I want to match a literal backslash. The other one works too, but I think ~\\\\~ is more clear.