note 50693 added to reference.pcre.pattern.syntax

From: Date: Mon, 07 Mar 2005 15:22:06 +0000
Subject: note 50693 added to reference.pcre.pattern.syntax
Groups: php.notes 
Request: Send a blank email to php-notes+get-86050@lists.php.net to get a copy of this message
Back references are a great way to achieve exact matching when it would have been impossible any other way. Take these three strings. 1) "www.www.com" 2) 'www.www.com' 3) "www.www.com' The regex /^("|').+?("|')$/ would match all three strings but what if you needed the 3rd string above to be illegal because the quotes are not the same? You could write four different regexes to check for every possible case OR you could use back references. /^("|').+?\1$/ will match strings 1 and 2 but not string 3. Try this code for further proof: $str_test="'www.www.com\""; $int_count=preg_match("/^(\"|').+?\\1$/", $str_test, $matches, PREG_OFFSET_CAPTURE); The preg_match function will not match against $str_test because the quotes are mismatched. If you change $str_test to $str_test = "'www.www.com'"; the preg_match will work. ---- Manual Page -- http://www.php.net/manual/en/reference.pcre.pattern.syntax.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50693 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50693&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50693&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50693&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50693&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50693&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50693&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86050) next »