RegEx 从 PHP 7.4 开始失败,在 7.3 中工作

Max*_*ens 6 php regex preg-match

有什么想法为什么这个 preg_match 可以在 PHP7.2 上运行但在 7.3+ 上失败?

$word = 'umweltfreundilch'; //real life example :/
preg_match('/^(?U)(.*(?:[aeiouyäöü])(?:[^aeiouyäöü]))(?X)(.*)$/u', $word, $matches);
var_dump($matches);
Run Code Online (Sandbox Code Playgroud)

警告:preg_match():编译失败:(? 或 (?-

PHP 7.2 及以下输出:

array(3) {
  [0]=>
  string(16) "umweltfreundilch"
  [1]=>
  string(2) "um"
  [2]=>
  string(14) "weltfreundilch"
}
Run Code Online (Sandbox Code Playgroud)

正则表达式似乎没问题,不是吗?
https://regex101.com/r/LGdhaM/1

Wik*_*żew 4

在 PHP 7.3 及更高版本中,Perl 兼容正则表达式 (PCRE) 扩展升级到 PCRE2。

\n

PCRE2语法文档未列为(?X)可用的内联修饰符选项。以下是支持的选项:

\n
  (?i)            caseless\n  (?J)            allow duplicate named groups\n  (?m)            multiline\n  (?n)            no auto capture\n  (?s)            single line (dotall)\n  (?U)            default ungreedy (lazy)\n  (?x)            extended: ignore white space except in classes\n  (?xx)           as (?x) but also ignore space and tab in classes\n  (?-...)         unset option(s)\n  (?^)            unset imnsx options\n
Run Code Online (Sandbox Code Playgroud)\n

但是,您实际上可以X在尾随定界符之后使用标志:

\n
preg_match(\'/^(?U)(.*[aeiouy\xc3\xa4\xc3\xb6\xc3\xbc][^aeiouy\xc3\xa4\xc3\xb6\xc3\xbc])(.*)$/Xu\', $word, $matches)\n
Run Code Online (Sandbox Code Playgroud)\n

请参阅PHP 7.4 演示

\n

要取消(?U)效果,您可以使用以下两个选项之一:(?-U)内联修饰符,例如

\n
preg_match(\'/^(?U)(.*[aeiouy\xc3\xa4\xc3\xb6\xc3\xbc][^aeiouy\xc3\xa4\xc3\xb6\xc3\xbc])(?-U)(.*)$/u\', $word, $matches);\n//                                           ^^^^^\n
Run Code Online (Sandbox Code Playgroud)\n

或者,将受影响的模式放入(?U:...)修饰符组中:

\n
preg_match(\'/^(?U:(.*[aeiouy\xc3\xa4\xc3\xb6\xc3\xbc][^aeiouy\xc3\xa4\xc3\xb6\xc3\xbc]))(.*)$/u\', $word, $matches);\n//            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        \n
Run Code Online (Sandbox Code Playgroud)\n

有关 PHP 7.3+ 中正则表达式处理更改的更多信息,请参阅preg_match(): Compilation failed: invalid range in character class at offset

\n