note 111935 added to regexp.reference.recursive

From: Date: Mon, 15 Apr 2013 12:07:53 +0000
Subject: note 111935 added to regexp.reference.recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-194476@lists.php.net to get a copy of this message
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))*\\))"; // (reuseable) $bracket_systems = "((?>[^()]+)?$bracket_system)*(?>[^()]+)?"; // (reuseable) $equation = "$bracket_systems=$bracket_systems"; // Both side of the equation must be contain valid bracket systems var_dump(preg_match("/^$equation\$/","a*(a-(2a+2))=4(a+3)-2(a-(a-2))")); // Outputs 'int(1)' var_dump(preg_match("/^$equation\$/","a*(a-(2a+2)=4(a+3)-2(a-(a-2)))")); // Outputs 'int(0)' ?> You can also catch multibyte quotes with the 'u' modifier (if you use UTF-8), eg: <?php $quoted = "(»((?>[^»«]+)|(?-2))*«)"; // (reuseable) $prompt = "[\\w ]+: $quoted"; var_dump(preg_match("/^$prompt\$/u","Your name: »write here«")); // Outputs 'int(1)' ?> ---- Server IP: 69.147.83.199 Probable Submitter: 86.101.233.13 ---- Manual Page -- http://php.net/manual/en/regexp.reference.recursive.php Edit -- https://master.php.net/note/edit/111935 Del: integrated -- https://master.php.net/note/delete/111935/integrated Del: useless -- https://master.php.net/note/delete/111935/useless Del: bad code -- https://master.php.net/note/delete/111935/bad+code Del: spam -- https://master.php.net/note/delete/111935/spam Del: non-english -- https://master.php.net/note/delete/111935/non-english Del: in docs -- https://master.php.net/note/delete/111935/in+docs Del: other reasons-- https://master.php.net/note/delete/111935 Reject -- https://master.php.net/note/reject/111935 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194476) next »