I'm using TDD and I have to pass a set of tests to implement a new library:
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. 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. 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. 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?