note 107520 added to regexp.reference.onlyonce

From: Date: Tue, 14 Feb 2012 22:16:59 +0000
Subject: note 107520 added to regexp.reference.onlyonce
Groups: php.notes 
Request: Send a blank email to php-notes+get-186139@lists.php.net to get a copy of this message
For what it's worth, here's the expression I came up with that (I think) matches everything the original does, and nothing else, while being efficient. ((?>[^\d<!?]+)|<((?>\d+)>)?|(?>[!?]+))*(?<=[!?]) It searches for any number of A: a series of anything besides a digit, <, !, or ?, and/or B: <, optionally followed by a series of digits and >, and/or C: one or more ? and/or ! . It then uses a look-behind to check that the end of the string is ! or ?. The series of digits, the series of (not-digit, not-<,!,?), and the series of ? and/or ! are all once-only. ((?>\D+)|<\d+>)*[!?] wouldn't work, because the once-only group could swallow up ! or ? at the end, and also the < that would start the alternate. The third group (?>[!?]+) is required in order to match the string when other characters (besides digits) follow the pattern. Example: aaaaa<123>!?!?!--- If the expression only needs to match a whole string, then the following will work. ^((?>[^\d<]+)|<((?>\d+)>)?)*(?<=[!?])$ ---- Server IP: 69.147.83.197 Probable Submitter: 76.201.100.232 ---- Manual Page -- http://www.php.net/manual/en/regexp.reference.onlyonce.php Edit -- https://master.php.net/note/edit/107520 Del: integrated -- https://master.php.net/note/delete/107520/integrated Del: useless -- https://master.php.net/note/delete/107520/useless Del: bad code -- https://master.php.net/note/delete/107520/bad+code Del: spam -- https://master.php.net/note/delete/107520/spam Del: non-english -- https://master.php.net/note/delete/107520/non-english Del: in docs -- https://master.php.net/note/delete/107520/in+docs Del: other reasons-- https://master.php.net/note/delete/107520 Reject -- https://master.php.net/note/reject/107520 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186139) next »