2016-02-23 111 views
-2

我做一個Perl的正則表達式的問題改變\字符遵循以下規則:Perl的正則表達式mathjax語法

  1. 的匹配序列應該用\(
  2. 開始它應與\)
  3. 任何\結束前一匹配序列中的字符應替換爲雙反斜槓\\

示例文本參考:

Se la \probabilità dell'evento\ A è \(\frac{3}{4} \) e la 
probabilità dell'evento B è \(\frac{1}{4}\)  
\(\frac{3}{4} +\frac{3}{4}\) . 
\(\frac{1}{4} - \frac{3}{4}\) . 
\(\frac{3}{16}\) . 
\(\frac{1}{2}\) . 

應該改爲:

Se la \probabilità dell'evento\ A è \\(\\frac{3}{4} \\) e la 
probabilità dell'evento B è \\(\\frac{1}{4}\\)  
\\(\\frac{3}{4} +\\frac{3}{4}\\) . 
\\(\\frac{1}{4} - \\frac{3}{4}\\) . 
\\(\\frac{3}{16}\\) . 
\\(\\frac{1}{2}\\) . 

到目前爲止,這是我最好的選擇:

s/(\\\()(.*)(\\)(.*)(\\\))/\\\\\($2\\\\$4\\\\\)/mg 

主要生產:

Se la \probabilità dell'evento\ A è \\(\\frac{3}{4} \\) e la 
probabilità dell'evento B è \\(\\frac{1}{4}\\)  
\\(\frac{3}{4} +\\frac{3}{4}\\) . 
\\(\frac{1}{4} - \\frac{3}{4}\\) . 
\\(\\frac{3}{16}\\) . 
\\(\\frac{1}{2}\\) . 

,你可以見

\\(\frac{3}{4} +\\frac{3}{4}\\) . 
\\(\frac{1}{4} - \\frac{3}{4}\\) . 

是錯誤的。

如何修改我的正則表達式以適應我的需求?

+4

這將是一個有效的問題,如果你的後樣品的輸入和輸出它,「我嘗試使用下面的代碼繼續:.......但未能匹配以下情況:.........。我應該如何修改我的代碼以適應這種情況呢?「正如所寫,這是一個需要完成的工作規範,而不是要求你正在處理的代碼的指導的問題。 – DavidO

+0

對不起,我忘了添加我的嘗試到目前爲止,我只是編輯了問題 – LaboDJ

+0

它可以用'$ string =〜s /(?x)(?:(?!\ A)\ G [^ \\] * \ K \\ | \\ (?= \())(?=。*?(?<= \\)\))/ \\\\/g;' – sln

回答

1

發佈從我原來的一個更新的正則表達式。

原來的驗證結果爲所有逃脫。
看了之後,它可以通過一次確認
找到開始塊來加快速度。

底部是一個比較兩種方法的基準。

更新的正則表達式:

$str =~ s/(?s)(?:(?!\A)\G(?!\))[^\\]*\K\\|\\(?=\(.*?\\\)))/\\\\/g;

Formatted and tested:

(?s)    # Dot-All modifier 
(?:    # Cluster start 
     (?! \A)   # Not beginning of string 
     \G     # G anchor - If matched before, start at end of last match 
     (?! \))   # Last was an escape, so ')' ends the block 
     [^\\]*    # Many non-escape's 
     \K     # Previous is not part of match 
     \\     # A lone escape 
    |     # or, 
         # New Block Check - 
     \\     # A lone escape then, 
     (?=    # One time Validation: 
      \(    # an opening '(' 
      .*?    # anything 
      \\ \)    # then a final '\)' 
    )     # ------------- 
)     # Cluster end 

基準:

樣品\(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \)

結果

New Regex: (?s)(?:(?!\A)\G(?!\))[^\\]*\K\\|\\(?=\(.*?\\\))) 
Options: <none> 
Completed iterations: 50/50  (x 1000) 
Matches found per iteration: 31 
Elapsed Time: 1.25 s, 1253.92 ms, 1253924 µs 


Old Regex: (?s)(?:(?!\A)\G[^\\]*\K\\|\\(?=\())(?=.*?(?<=\\)\)) 
Options: <none> 
Completed iterations: 50/50  (x 1000) 
Matches found per iteration: 31 
Elapsed Time: 3.95 s, 3952.31 ms, 3952307 µs 
1

我測試@sln正則表達式

s/(?x)(?:(?!\A)\G[^\\]*\K\\|\\(?=\())(?=.*?(?<=\\)\))/\\\\/g; 

而且似乎工作,但它仍然是一個神祕的謎給我。

更新與解釋

Formatted and tested:

(?s)    # Inline Dot-All modifier 
(?:    # Cluster start 
     (?! \A)   # Not beginning of string 
     \G     # G anchor - If matched before, start at end of last match 
     [^\\]*    # Many non-escape's 
     \K     # Previous is not part of match 
     \\     # A lone escape 
    |     # or, 
         # Start of an opening '\(' 
     \\     # A lone escape 
     (?= \()   # followed by an open parenth 
)     # Cluster end 
(?=    # Lookahead, each match validates a final '\)' 
     .*? 
     (?<= \\) 
     \) 
) 
+1

將'(?x)'改爲'(?s)'這並不是一個謎,我會更新你的文章一個解釋。 – sln

+1

我發佈了一個更快的變體螞蟻正則表達式,如果你有興趣。 – sln

+0

@sln是的,非常感謝 – LaboDJ