问题描述
考虑(\w[0-9]).*\1
RegEx,它匹配d1akdhfafd1
、R2ddsfasfasfdsfdR2
等.
Consider (\w[0-9]).*\1
RegEx, it matches to d1akdhfafd1
, R2ddsfasfasfdsfdR2
, etc. .
是否可以编写与以下内容匹配的正则表达式:D1dfsadfadsfE3
、z6adfdasfdfr2
、e3654654e0
、....
Is is possible to write a RegEx that that match to following too: D1dfsadfadsfE3
, z6adfdasfdfr2
, e3654654e0
,....?
\w[0-9]
只是一个例子,请考虑一般形式 (::A_Complex_Pattern::).*\1
\w[0-9]
is just an example, please consider general form (::A_Complex_Pattern::).*\1
推荐答案
PHP 的正则表达式引擎 (PCRE) 支持 递归.其他一些人这样做,但您通常不应该指望它.但是,如果引擎这样做,那么您可以像这样插入其他地方使用的模式:
PHP's regex engine (PCRE) supports recursion. A few others do, but you generally shouldn't count on it. However, if the engine does, then you can insert patterns used elsewhere like this:
(\w\d).*(?1)
其中编号与反向引用的编号相同.(?R)
将插入整个模式.
Where the numbering as the same as that for backreferences. (?R)
would insert the whole pattern.
这篇关于使用反向引用来反向引用模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!