简体   繁体   English

正则表达式:选择内的反向引用

[英]Regular expression: back reference inside the selection

I'm using TDD and I have to pass a set of tests to implement a new library: 我正在使用TDD,并且必须通过一组测试才能实现新的库:

public function providerEdgesParser()
{
    return array(
        array('.edges=(user)', false), // 0
        array('edges=test', false),
        array('another:chars', false),
        array('pl-ouf', false),
        array('test', array('test')),
        array('lang,lang', array('lang', 'lang')), // 5
        array('quest,ans', array('quest', 'ans')),
        array('q.edges=(a)', array('q' => array('a'))),
        array('e.edges=(lang,et.edges=(lang)),ans', array('e' => array('lang', 'et' => array('lang')), 'ans')),
    );
}

This is a PHPUnit provider. 这是一个PHPUnit提供程序。 In each array, first element is the parameter of my function, second element is what my function must return. 在每个数组中,第一个元素是我的函数的参数,第二个元素是我的函数必须返回的内容。 Here is this function I've come up with: 这是我想出的这个功能:

public function edgesParser($urlEdges)
{
      // Check if edges syntax is valid
      if (!preg_match('#^((?:(?:[a-z]+(?:\.edges\=\(\1\))?)\,?)+)$#ui', $urlEdges)) {
            throw new \Exception('Edges syntax is wrong');
      }

      // Then, use a recursive function to build the array
      // ...
      // ...
}

The only purpose of that regular expression is to detect bad syntax in the $urlEdges string, as it is an end user input. 该正则表达式的唯一目的是检测$urlEdges字符串中的语法$urlEdges ,因为它是最终用户的输入。 Only after, I will build the right array to return. 只有在此之后,我才会构建正确的数组以返回。

However, this regex doesn't seems to work the way I want: the two lastest tests throw an Exception. 但是,此正则表达式似乎无法按照我想要的方式工作:最近的两个测试均引发Exception。 They should not. 他们不应该。

I have been searching for a solution for a long time, but I just can't see where the regular expression is wrong. 我一直在寻找解决方案很长时间,但是我看不到正则表达式在哪里出错。 Here is a graphical representation of the regex . 这是正则表达式的图形表示 Could it back reference don't work when it's inside the referred group? 当它在被引用的组中时,回引用不能工作吗? Or did I make a trivial error that my tired eyes can't see? 还是我犯了一个琐碎的错误,我的疲倦的眼睛看不见?

@HamZa brought the answer. @HamZa带来了答案。

\\1 back reference matchs what was matched in group 1. \\1 后向引用与组1中的匹配。

(?1) recursive mask executes the pattern from group 1. (?1) 递归掩码执行组1中的模式。

The second option is what I needed. 第二个选择是我需要的。 So, a suitable regex could be: #^((?:(?:[az]+(?:\\.edges\\=\\((?1)\\))?)\\,?)+)$#ui (split up here ). 因此,合适的正则表达式可能是: #^((?:(?:[az]+(?:\\.edges\\=\\((?1)\\))?)\\,?)+)$#ui (在这里拆分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM