note 50693 deleted from reference.pcre.pattern.syntax by crell

From: Date: Fri, 01 Jul 2022 20:09:12 +0000
Subject: note 50693 deleted from reference.pcre.pattern.syntax by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218880@lists.php.net to get a copy of this message
Note Submitter: W W W ---- 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.

« previous php.notes (#218880) next »