使用先前的反向引用作为命名捕获组的名称

问题描述 投票:5回答:2

有没有办法使用对先前捕获组的反向引用作为命名捕获组的名称?这可能是不可能的,如果没有,那么这是一个有效的答案。

下列:

$data = 'description: some description';
preg_match("/([^:]+): (.*)/", $data, $matches);
print_r($matches);

产量:

(
    [0] => description: some description
    [1] => description
    [2] => some description
)

我尝试使用第一个捕获组的反向引用作为命名捕获组(?<$1>.*)告诉我它不可能或者我只是没有正确地执行它:

preg_match("/([^:]+): (?<$1>.*)/", $data, $matches);

产量:

警告:preg_match():编译失败:无法识别的字符(?<在偏移量12处

期望的结果是:

(
    [0] => description: some description
    [1] => description
    [description] => some description
)

使用preg_match简化了这一过程。当使用preg_match_all时,我通常使用:

$matches = array_combine($matches[1], $matches[2]);

但我想我可能比那更光滑。

php regex pcre regex-group backreference
2个回答
4
投票

简而言之,这是不可能的,你可以坚持到目前为止你一直在使用的编程方式。

组编号(应该是consist of up to 32 alphanumeric characters and underscores, but must start with a non-digit)在编译时被解析,而反向引用值仅在运行时被识别。请注意,这也是为什么你不能在lookbehind内部使用反向引用的原因(尽管你清楚地看到/(x)y[a-z](?<!\1)/是正常的,PCRE regex engine sees otherwise因为它无法通过反向引用来推断lookbehind的长度)。


2
投票

您已经有了正则表达式问题的答案(否),但对于不同的基于PHP的方法,您可以尝试使用回调。

preg_replace_callback($pattern, function($match) use (&$matches) {
    $matches[$match[1]] = $match[2];
}, $data);
© www.soinside.com 2019 - 2024. All rights reserved.