gpt4 book ai didi

php - Regex\R 在字符类中不起作用

转载 作者:可可西里 更新时间:2023-11-01 13:14:30 25 4
gpt4 key购买 nike

在 PHP 中,应该匹配任何换行序列的转义字符 \R 在字符类中不起作用。

我最近在 stackoverflow 上的另一个答案中了解了这个特殊字符,老实说,我无法在网上找到太多来记录它的存在 - 除了在用户评论中之外,在 php.net 上没有任何地方提到它。

问题:

  • 为什么 \R 不能在字符类中工作?
  • 记录在哪里?

示例 1: ( https://regex101.com/r/vA8xV3/3 )

$a = "line1
line2"

echo preg_replace('/\R/',' ',$a);

返回(找到匹配项,替换为单个空格):

line1 line2

示例 2: ( https://regex101.com/r/vA8xV3/2 )

$a = "line1
line2"

echo preg_replace('/[\R]/',' ',$a);

返回(不匹配):

line1
line2

最佳答案

来自PCRE manual :

Escape sequences in character classes

All the sequences that define a single character value can be used both inside and outside character classes. In addition, inside a characterclass, \b is interpreted as the backspace character (hex 08).

\N is not allowed in a character class. \B, \R, and \X are not special inside a character class. Like other unrecognized escape sequences,they are treated as the literal characters "B", "R", and "X" by default, but cause an error if the PCRE_EXTRA option is set. Outside acharacter class, these sequences have different meanings.

(强调我添加的相关位)

关于php - Regex\R 在字符类中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30103145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com