永不结束的do-while循环使用正则表达式


Never ending do-while loop with regex

我正在尝试在XLIFF文件上运行一系列模式。示例:

  <trans-unit id="1">
    <source> I like "sausages". </source>
    <target> J'aime bien les « sausices » </target>
  </trans-unit>
  <trans-unit id="2">
    <source> I like "sausages". </source>
    <target> J'aime bien les «sausices» </target>
  </trans-unit>
我解析文件,然后在每个目标元素上运行每个模式。
    foreach($patterns as $p) {
        if (preg_match($p['find'], $tu[0]->target, $dummy)) {
            do {
                $targetText = $tu[0]->target;
                $tu[0]->target = preg_replace($p['find'], $p['repl'], $targetText, -1, $count);
            } while ($count);
        }
    }
例如,我有一个数组的模式:
        $patterns[1] = array(
            'find' => "/[«‹]'K(?!'x{00A0})'s/imu",
            'repl' => "&#8239;"
            );
        $patterns[2] = array(
            'find' => "/[«‹]'K(?!'p{Zs})/imu",
            'repl' => "&#8239;"
            );

模式1应该匹配上面的跨单元1,模式2应该匹配跨单元2。模式1工作得很好,但如果我运行模式2(只运行或同时运行),循环就永远不会结束。这个替换基本上是用一个狭窄的分隔符替换«或»(模式1)之后的正常(分隔符)空格,或者在根本没有空格的情况下插入它(模式1)。

我想说这个问题与第二个正则表达式有关,但我不知道那个表达式有什么问题。任何建议吗?

'p{Zs}模式与&#8239;不匹配,因此将&#8239;添加到第二个模式的forward条件中:

'find' => "/[«‹]'K(?!'p{Zs}|&#8239;)/iu",) 
                            ^^^^^^^