Regex-PHP中的反斜杠

我正在尝试用PHP学习正则表达式并且现在停留在这里。 我的问题可能看起来很傻,但请解释一下。

我通过一个链接:

PHP正则表达式模式中需要额外的反斜杠 </ p>

但我无法理解:</ p>

在 回答他提到了两个陈述:</ p>

2个反斜杠用于字符串中的unescaping(“\\”</ code> - &gt; \ </ code>)

1反斜杠用于在regex引擎中进行unescaping( \ </ code> - &gt; \ </ code>)</ p>

我的问题:

“nnescaping”这个词实际上意味着什么? unescaping的目的是什么?
为什么我们需要4个反斜杠才能将它包含在正则表达式中?</ p>
</ div>

展开原文

原文

I am trying to learn Regex in PHP and stuck in here now. My ques may appear silly but pls do explain.
I went through a link:
Extra backslash needed in PHP regexp pattern

But I just could not understand something:

In the answer he mentions two statements:

2 backslashes are used for unescaping in a string ("\\\\" -> \\)
1 backslash is used for unescaping in the regex engine (\\ -> \)

My ques:
what does the word "unescaping" actually means? what is the purpose of unescaping? Why do we need 4 backslashes to include it in the regex?

dongzhunqiu4841
dongzhunqiu4841 因为他想要\而不是\
6 年多之前 回复
dth8312
dth8312 那么两个反斜杠可以完成这项工作,为什么我们包括4个反斜杠?
6 年多之前 回复
dream8877
dream8877 它叫做逃避不?转义是必需的,因为某些字符被定义为具有各自功能的预先设置的字符。为了让PHP知道你想要真正的角色,而不是预先设定的角色,你需要逃避它,所以PHP不要像特殊的那样对待
6 年多之前 回复

2个回答



反斜杠在regexen 和</ em> PHP中都有特殊含义。 在这两种情况下,它都用作转义字符。 例如,如果要在PHP字符串文字中写一个文字引号字符,这将不起作用:</ p>

  $ str ='''; 
</ code > </ pre>

PHP会“混淆”,'</ code>结束字符串,这是字符串的一部分。 那就是 \ </ code>的来源:</ p>

  $ str ='\''; 
</ code> </ pre>

< p>它转义</ em> '</ code>的特殊含义,因此它不是终止字符串文字,而是现在只是字符串中的普通字符。 还有更多的转义序列,如
</ code>。</ p>

现在这意味着 \ </ code>是一个具有特殊含义的特殊字符。 要想在编写文字 \ </ code>时逃避这个难题,你必须将文字反斜杠转义为 \ </ code>:</ p>

   $ str ='\';  //表示一个反斜杠的字符串文字
</ code> </ pre>

这在PHP和regexen中都是一样的。 如果要在正则表达式中编写文字反斜杠,则必须编写 / \ / </ code>。 现在,既然你把你的regexen写成PHP字符串,你需要双重转义:</ p>

  $ regex ='/ \\ /'; 
</ 代码> </ pre>

PHP字符串转义机制首先将一对 \ </ code>简化为一个 \ </ code>,所以实际的正则表达式是 / \ / </ code>,这是一个正则表达式,意思是“一个反斜杠”。</ p>
</ div>

展开原文

原文

The backslash has a special meaning in both regexen and PHP. In both cases it is used as an escape character. For example, if you want to write a literal quote character inside a PHP string literal, this won't work:

$str = ''';

PHP would get "confused" which ' ends the string and which is part of the string. That's where \ comes in:

$str = '\'';

It escapes the special meaning of ', so instead of terminating the string literal, it is now just a normal character in the string. There are more escape sequences like as well.

This now means that \ is a special character with a special meaning. To escape this conundrum when you want to write a literal \, you'll have to escape literal backslashes as \\:

$str = '\\'; // string literal representing one backslash

This works the same in both PHP and regexen. If you want to write a literal backslash in a regex, you have to write /\\/. Now, since you're writing your regexen as PHP strings, you need to double escape them:

$regex = '/\\\\/';

One pair of \\ is first reduced to one \ by the PHP string escaping mechanism, so the actual regex is /\\/, which is a regex which means "one backslash".



我认为你可以使用“preg_quote()”:</ p>

http://php.net/preg_quote </ p>

此函数可以逃避特殊字符,因此您可以 按原样输入,不自行转义:</ p>

 &lt;?php 
$ string =“online 24/7。仅用于\ o /”;
$ escaped_string = preg_quote($ string,“/”); //第二个param是可选的,如果你想要转义regex
echo $ escaped_string的分隔符,也可以使用它; // $ escaped_string:“online 24 \ / 7.仅适用于\ o \ /”
?&gt;
</ code> </ pre>
</ div>

展开原文

原文

I think you can use "preg_quote()":

http://php.net/preg_quote

This function escapes special chars, so you can give an input as it is, without escaping by yourself:

<?php
$string = "online 24/7. Only for \o/";
$escaped_string = preg_quote($string, "/"); // 2nd param is optional and used if you want to escape also the delimiter of your regex
echo $escaped_string; // $escaped_string: "online 24\/7.  Only for \\o\/"
?>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问