如何在单引号和双引号PHP正则表达式模式中正确地转义反斜杠以匹配文字反斜杠 [英] How to properly escape a backslash to match a literal backslash in single-quoted and double-quoted PHP regex patterns

查看:88
本文介绍了如何在单引号和双引号PHP正则表达式模式中正确地转义反斜杠以匹配文字反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了匹配字面反斜线,许多人和 PHP手册说:总是像这样\\\\

To match a literal backslash, many people and the PHP manual say: Always triple escape it, like this \\\\

注意:

单引号和双引号的PHP字符串具有反斜杠的特殊含义.因此,如果\必须与正则表达式\\匹配,则必须在PHP代码中使用"\\\\"'\\\\'.

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.

下面是一个示例字符串:\test

Here is an example string: \test

$test = "\\test"; // outputs \test;

// WON'T WORK: pattern in double-quotes double-escaped backslash
#echo preg_replace("~\\\t~", '', $test); #output -> \test

// WORKS: pattern in double-quotes with triple-escaped backslash
#echo preg_replace("~\\\\t~", '', $test); #output -> est

// WORKS: pattern in single-quotes with double-escaped backslash
#echo preg_replace('~\\\t~', '', $test); #output -> est

// WORKS: pattern in double-quotes with double-escaped backslash inside a character class
#echo preg_replace("~[\\\]t~", '', $test); #output -> est

// WORKS: pattern in single-quotes with double-escaped backslash inside a character class
#echo preg_replace('~[\\\]t~', '', $test); #output -> est

结论:

  • 如果模式是单引号,则必须对反斜杠进行两次转义\\\,以匹配文字\
  • 如果模式被双引号,则取决于是否 反冲在字符类内部,必须至少两次转义\\\ 在字符类之外,必须将其三重转义\\\\
  • If the pattern is single-quoted, a backslash has to be double-escaped \\\ to match a literal \
  • If the pattern is double-quoted, it depends whether the backlash is inside a character-class where it must be at least double-escaped \\\ outside a character-class it has to be triple-escaped \\\\

谁能给我带来不同,其中以单引号引起来的双转义反斜杠例如'~\\\~'将匹配与双引号模式中的三转义反斜杠不同的任何内容,例如"~\\\\~"或失败.

Who can show me a difference, where a double-escaped backslash in a single-quoted pattern e.g. '~\\\~' would match anything different than a triple-escaped backslash in a double-quoted pattern e.g. "~\\\\~" or fail.

何时/为什么/在什么情况下以单引号引起来的双转义\是错误的,例如'~\\\~'用于匹配文字反斜杠?

When/why/in what scenario would it be wrong to use a double-escaped \ in a single-quoted pattern e.g. '~\\\~' for matching a literal backslash?

如果这个问题没有答案,我将继续在单引号PHP regex模式中始终使用双转义的反斜杠\\\来匹配文字\,因为它可能没有问题.

If there's no answer to this question, I would continue to always use a double-escaped backslash \\\ in a single-quoted PHP regex pattern to match a literal \ because there's possibly nothing wrong with it.

推荐答案

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.

这篇关于如何在单引号和双引号PHP正则表达式模式中正确地转义反斜杠以匹配文字反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆