2016-11-17 69 views
1

我試圖在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); 
     } 
    } 

例如,我有patters的數組:

 $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)。

我會說這個問題與第二個正則表達式有關,但我無法弄清楚這個表達式有什麼問題。有小費嗎?

+0

有時人們樣本數據添加到他們的問題.... – Andreas

+0

對不起,我我以前不覺得這將是相關的。現在補充,謝謝! – msoutopico

+0

與這個問題無關,但在法國它寫的'saucisse' – Toto

回答

1

\p{Zs}模式不匹配&#8239;,因此在第二圖案添加到&#8239;先行條件:

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

正如我在11月17日的評論中發表的那樣。Thans,Wiktor :) – msoutopico