With the (?R) item you can link only to the full pattern, because it quasi equals to (?0). You can not use anchors, asserts etc., and you can only check that string CONTAINS a valid hierarchy or not.
This is wrong: ^\(((?>[^()]+)|(?R))*\)$
However, you can bracketing the full expression, and replace (?R) to the relative link (?-2). This make it reusable. So you can check complex expressions, for example:
<?php
$bracket_system = "(\\(((?>[^()]+)|(?-2))*\\))"; $bracket_systems = "((?>[^()]+)?$bracket_system)*(?>[^()]+)?"; $equation = "$bracket_systems=$bracket_systems"; var_dump(preg_match("/^$equation\$/","a*(a-(2a+2))=4(a+3)-2(a-(a-2))")); var_dump(preg_match("/^$equation\$/","a*(a-(2a+2)=4(a+3)-2(a-(a-2)))")); ?>
You can also catch multibyte quotes with the 'u' modifier (if you use UTF-8), eg:
<?php
$quoted = "(»((?>[^»«]+)|(?-2))*«)"; $prompt = "[\\w ]+: $quoted";
var_dump(preg_match("/^$prompt\$/u","Your name: »write here«")); ?>