@@ -1336,6 +1336,8 @@ private function regexp_check_match_type( $function_name, $match_type ) {
13361336 * - ICU attaches a combining mark to the character before it, so a mark after a
13371337 * space doesn't start a word for `\b`. Here, it does.
13381338 * - `\G` in a lookbehind doesn't always match where it does in ICU.
1339+ * - A `#` comment of the `x` flag is translated like the rest of the pattern, so
1340+ * an invalid set in it is an error, and a `\Q` in it quotes the text after it.
13391341 * - Data that isn't valid UTF-8 is matched byte by byte and case-sensitively,
13401342 * like MySQL binary strings. Other binary values match as text, so case
13411343 * sensitivity, and whether positions count bytes, can differ between rows.
@@ -1447,15 +1449,16 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
14471449 /**
14481450 * Translate a MySQL regex pattern to PCRE syntax.
14491451 *
1450- * Splits the pattern into quoted text, sets, escapes, POSIX classes, and other
1451- * characters, so that a translation applies only to a whole token.
1452+ * Splits the pattern into quoted text, comments, sets, escapes, POSIX classes, and
1453+ * other characters, so that a translation applies only to a whole token.
14521454 *
14531455 * @param string $pattern The regex pattern.
14541456 * @param bool $case_insensitive Whether the pattern ignores case by default.
14551457 * @param bool $binary_mode Whether the pattern matches bytes instead of UTF-8 characters.
14561458 * @param bool $without_g Whether "\G" never matches.
14571459 *
1458- * @throws Exception If the pattern can't be split, or it has an invalid set.
1460+ * @throws Exception If the pattern can't be split, or it has an invalid set or a
1461+ * comment without an end.
14591462 * @return string Pattern safe to wrap in slash delimiters.
14601463 */
14611464 private function regexp_translate_pattern ( $ pattern , $ case_insensitive , $ binary_mode , $ without_g ) {
@@ -1506,6 +1509,10 @@ private function regexp_translate_pattern( $pattern, $case_insensitive, $binary_
15061509 }
15071510
15081511 /*
1512+ * A "(?#...)" comment is one token, so that no translation changes it. Like in
1513+ * ICU, a "\Q" in it quotes the text up to "\E", which can hold its ")", but "\\Q"
1514+ * is an escaped backslash and a "Q".
1515+ *
15091516 * A set, with its nested sets, is one token, as ICU's sets differ from PCRE's.
15101517 * Like ICU, "[:" starts a POSIX class if its name, which ends at the next ":",
15111518 * is followed by "]", and a set otherwise. ICU reads a POSIX class outside of a
@@ -1518,12 +1525,19 @@ private function regexp_translate_pattern( $pattern, $case_insensitive, $binary_
15181525 * scan the rest of the pattern each time.
15191526 */
15201527 $ translated = preg_replace_callback (
1521- '/ \\\\Q.*?(?: \\\\E|\z)|\[:\^?[^:]*:\]|(\[\^?+\]?+(?: \\\\Q.*?(?: \\\\E|\z)| \\\\c.| \\\\.|\[:\^?[^:]*:\]|(?1)|[^ \\\\\[\]])*+\])| ' . self ::REGEXP_CODE_POINT_ESCAPE . '| \\\\[pP]\{[^} \\\\]*\}| \\\\.|[\/\0]/s ' ,
1528+ '/ \\\\Q.*?(?: \\\\E|\z)|\(\?#(?: \\\\ Q.*?(?: \\\\ E|\z)| \\\\ [^)]|[^)])*+\)|\(\?#|\ [:\^?[^:]*:\]|(\[\^?+\]?+(?: \\\\Q.*?(?: \\\\E|\z)| \\\\c.| \\\\.|\[:\^?[^:]*:\]|(?1)|[^ \\\\\[\]])*+\])| ' . self ::REGEXP_CODE_POINT_ESCAPE . '| \\\\[pP]\{[^} \\\\]*\}| \\\\.|[\/\0]/s ' ,
15221529 function ( $ parts ) use ( $ pattern , $ tokens ) {
15231530 $ token = $ parts [0 ];
15241531 if ( isset ( $ parts [1 ] ) ) {
15251532 return $ this ->regexp_translate_set ( $ token , $ pattern , $ tokens );
15261533 }
1534+ if ( '(?# ' === $ token ) {
1535+ // The comment has no end, as its ")" is missing or quoted.
1536+ throw new Exception ( "Invalid regular expression: $ pattern. " );
1537+ }
1538+ if ( '( ' === $ token [0 ] ) {
1539+ return '(?#) ' ;
1540+ }
15271541 if ( '[ ' === $ token [0 ] ) {
15281542 return '[ ' . $ this ->regexp_translate_property ( $ token , $ tokens ) . '] ' ;
15291543 }
@@ -1948,7 +1962,8 @@ private function regexp_matches( $pattern, $match_type, $byte_mode, $subject, $o
19481962 /*
19491963 * Only PCRE's "\K" can start a match before the search, and then the next
19501964 * search would find the same match forever. The translation reads "\K" as
1951- * "K", but not after a "\Q" in a comment, which it reads as quoted text.
1965+ * "K", but not after a "\Q" in an extended comment, which it reads as quoted
1966+ * text.
19521967 */
19531968 if ( $ match [0 ][1 ] < $ offset ) {
19541969 throw new Exception ( 'Invalid regular expression: ' . $ pattern . '. ' );
0 commit comments