PHP正则表达式模式中需要额外的反斜杠 [英] Extra backslash needed in PHP regexp pattern

查看:119
本文介绍了PHP正则表达式模式中需要额外的反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试另一个用户的问题的答案时,我发现了一些我不知道的东西理解.问题是用单个空格替换字符串中的所有文字\t \n \r字符.

When testing an answer for another user's question I found something I don't understand. The problem was to replace all literal \t \n \r characters from a string with a single space.

现在,我尝试的第一个模式是:

Now, the first pattern I tried was:

/(?:\\[trn])+/

令人惊讶的是没有用.我在Perl中尝试了相同的模式,但效果很好.经过一番尝试和错误后,我发现PHP希望该模式匹配3或4个反斜杠,如下所示:

which surprisingly didn't work. I tried the same pattern in Perl and it worked fine. After some trial and error I found that PHP wants 3 or 4 backslashes for that pattern to match, as in:

/(?:\\\\[trn])+/

/(?:\\\[trn])+/

这些模式-令我惊讶的是-两者都有效.为什么这些多余的反斜杠是必需的?

these patterns - to my surprise - both work. Why are these extra backslashes necessary?

推荐答案

您需要4个反斜杠来表示正则表达式中的1个,因为:

You need 4 backslashes to represent 1 in regex because:

  • 2个反斜杠用于对字符串("\\\\" -> \\)进行转义
  • 1个反斜杠用于在正则表达式引擎(\\ -> \)中进行转义
  • 2 backslashes are used for unescaping in a string ("\\\\" -> \\)
  • 1 backslash is used for unescaping in the regex engine (\\ -> \)

在PHP文档中,

转义任何其他字符也会导致反斜杠也被打印 1

escaping any other character will result in the backslash being printed too1

因此\\\[

  • 1个反斜杠用于对\进行转义,因为\[无效("\\\[" -> \\[)
  • 1个反斜杠用于在正则表达式引擎(\\[ -> \[)中进行转义
  • 1 backslash is used for unescaping the \, one stay because \[ is invalid ("\\\[" -> \\[)
  • 1 backslash is used for unescaping in the regex engine (\\[ -> \[)

是的,但是不是一个好习惯.

Yes it works, but not a good practice.

这篇关于PHP正则表达式模式中需要额外的反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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