@@ -115,6 +115,19 @@ public static function register_for( $pdo ): self {
115115 */
116116 private const REGEXP_CODE_POINT_ESCAPE = '\\\\u[dD][89abAB][0-9a-fA-F]{2} \\\\u[dD][c-fC-F][0-9a-fA-F]{2}| \\\\u[0-9a-fA-F]{4}| \\\\U[0-9a-fA-F]{8}| \\\\0(?:[0-3][0-7]{0,2}|[4-7][0-7]?) ' ;
117117
118+ /**
119+ * Letters that ICU reads literally after a backslash, outside of a set. PCRE rejects
120+ * most of them, and reads some differently, like "\K", which moves the start of a
121+ * match, even to before the place where the search started.
122+ */
123+ private const REGEXP_LITERAL_ESCAPES = 'gijlmoqyCEFIJKLMOTY ' ;
124+
125+ /**
126+ * Letters that ICU reads literally after a backslash, in a set. PCRE rejects most of
127+ * them, and reads some differently, like "\b".
128+ */
129+ private const REGEXP_LITERAL_SET_ESCAPES = 'bgijklmoqyzABCEFGIJKLMORTXYZ ' ;
130+
118131 /** @var string[] Compiled PCRE patterns, keyed by byte mode, "\G" variant, match type, and pattern. */
119132 private $ regexp_cache = array ();
120133
@@ -1245,9 +1258,9 @@ private function regexp_check_match_type( $function_name, $match_type ) {
12451258 * (UTF-8) modifier is applied unless byte-oriented matching is requested.
12461259 * Case-insensitive is the default.
12471260 *
1248- * MySQL uses ICU, which differs from PCRE in some syntax, like ICU-only escapes.
1249- * The known failures of the REGEXP test suites list these cases. Other known
1250- * differences:
1261+ * MySQL uses ICU, which differs from PCRE in some syntax, like `\N{...}` and
1262+ * property names. The known failures of the REGEXP test suites list these cases.
1263+ * Other known differences:
12511264 *
12521265 * - Text matching ignores case unless the `c` flag is passed, as the column
12531266 * collation isn't known here.
@@ -1311,9 +1324,9 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
13111324 }
13121325
13131326 /*
1314- * PHP 7 can read an unknown escape as literal text. For "\y", that matches ICU,
1315- * but ICU rejects "\u004", which has too few digits. The "X" modifier makes all
1316- * of them errors, like in PHP 8.
1327+ * PHP 7 can read an unknown escape as literal text. ICU rejects the ones that are
1328+ * left after the translation, like "\u004", which has too few digits. The "X"
1329+ * modifier makes them errors, like in PHP 8.
13171330 */
13181331 $ modifiers = $ binary_mode ? 'X ' : 'uX ' ;
13191332 if ( ! $ case_sensitive ) {
@@ -1385,12 +1398,6 @@ private function regexp_translate_pattern( $pattern, $case_insensitive, $without
13851398 // PCRE's [:punct:] also holds ASCII symbols like "+", where ICU's doesn't.
13861399 '[:punct:] ' => '\\p{P} ' ,
13871400 '[:^punct:] ' => '\\P{P} ' ,
1388-
1389- /*
1390- * ICU reads "\K" as "K". In PCRE, "\K" moves the start of the match, even to
1391- * before the place where the search started, like in "(?<=\Ka)".
1392- */
1393- '\\K ' => 'K ' ,
13941401 );
13951402 if ( $ without_g ) {
13961403 // A position can't be both a word boundary and not one, so "\b\B" never
@@ -1489,7 +1496,7 @@ private function regexp_translate_set( $set, $pattern, $tokens ) {
14891496 private function regexp_parse_set ( array $ parts , &$ position , $ pattern , $ tokens ) {
14901497 // An escape is translated, and another character is escaped if PCRE needs it.
14911498 $ literal = function ( $ part ) use ( $ tokens ) {
1492- return '\\' === $ part [0 ] ? $ this ->regexp_translate_token ( $ part , $ tokens ) : ( $ tokens [ $ part ] ?? preg_quote ( $ part ) );
1499+ return '\\' === $ part [0 ] ? $ this ->regexp_translate_token ( $ part , $ tokens, true ) : ( $ tokens [ $ part ] ?? preg_quote ( $ part ) );
14931500 };
14941501
14951502 $ negated = '[^ ' === $ parts [ $ position ++ ];
@@ -1624,10 +1631,11 @@ private function regexp_set_fragment( array $set ) {
16241631 *
16251632 * @param string $token The token.
16261633 * @param string[] $tokens Translations of tokens.
1634+ * @param bool $in_set Whether the token is in a set.
16271635 *
16281636 * @return string The PCRE equivalent.
16291637 */
1630- private function regexp_translate_token ( $ token , $ tokens ) {
1638+ private function regexp_translate_token ( $ token , $ tokens, $ in_set = false ) {
16311639 if ( '\\Q ' === substr ( $ token , 0 , 2 ) ) {
16321640 // Quoting can't hold the delimiter or NUL bytes, so they are escaped between
16331641 // two quotes. Quoted text that runs to the end gets an "\E", so that a trailing
@@ -1641,6 +1649,10 @@ private function regexp_translate_token( $token, $tokens ) {
16411649 );
16421650 return '\\E ' === substr ( $ token , -2 ) ? $ quoted : $ quoted . '\\E ' ;
16431651 }
1652+ $ letters = $ in_set ? self ::REGEXP_LITERAL_SET_ESCAPES : self ::REGEXP_LITERAL_ESCAPES ;
1653+ if ( 2 === strlen ( $ token ) && '\\' === $ token [0 ] && false !== strpos ( $ letters , $ token [1 ] ) ) {
1654+ return $ token [1 ];
1655+ }
16441656 if ( isset ( $ tokens [ $ token ] ) ) {
16451657 return $ tokens [ $ token ];
16461658 }
0 commit comments