For the reason why \R not allowed inside the character class, and \d , \s , \w , ... are allowed inside the character class, this is because \R can match CR LF ( \r\n ), which consists of 2 code points. For the same reason that \X not allowed inside a character class, since it corresponds to a grapheme cluster that can contain multiple code points.
It is assumed that the character class must correspond to only one code / code, which makes it a deterministic construct in the sense that it does not require backtracking. Allowing the sequence of code points / code block to correspond to the character class leads to the fact that the character class has a variable length, complicates the analysis of the minimum length / maximum length, which is used in several optimization options. It also requires a change in the semantics of correspondence. For example, given [\r\n\R] , should it match \r\n in the string "\r\n" , or should it follow the declared order and match only \R ? In case of failure, will we be allowed to retreat?
I am not sure about the implementation of PCRE. However, in Java, length analysis is used to optimize the repetition construct (for example, with a repetition of a fixed-length construct, you do not need to store the number of characters matched in each repetition for backtracking), optimize the case when the input string does not satisfy the minimum length requirements and determine whether the expression is allowed in look-behind or not.
source share