簡體   English   中英

c# - 如何在正則表達式替換中轉義特殊字符?

[英]How to escape special characters in a regex replace in c#?

我有一個包含字符串的文本文件

<disp-formula id="deqn*"><text-notation="math">\begin{equation*}
x=5 \tag{5}
y=3 \tag{6}
x+y=8 \tag {7}
\end{equation*}</text-notation="math"></disp-formula>

<disp-formula id="deqn*"><text-notation="math">\begin{equation*}
x+y=5 \tag{3}
\end{equation*}</text-notation="math"></disp-formula>

<disp-formula id="deqn*"><text-notation="math">\begin{equation*}
a+y=15 \tag {4a}
\end{equation*}</text-notation="math"></disp-formula>

<disp-formula id="deqn*"><text-notation="math">\begin{equation*}
x=5 \tag {9a}
y=3 \tag{10}
x+y=8 \tag{11}
\end{equation*}</text-notation="math"></disp-formula>
...etc

我正在嘗試將它們轉換為

<disp-formula id="deqn5-7"><text-notation="math">\begin{equation*}
x=5 \tag{5}
y=3 \tag{6}
x+y=8 \tag {7}
\end{equation*}</text-notation="math"></disp-formula>

<disp-formula id="deqn3"><text-notation="math">\begin{equation*}
x+y=5 \tag{3}
\end{equation*}</text-notation="math"></disp-formula>

<disp-formula id="deqn4a"><text-notation="math">\begin{equation*}
a+y=15 \tag {4a}
\end{equation*}</text-notation="math"></disp-formula>

<disp-formula id="deqn9a-11"><text-notation="math">\begin{equation*}
x=5 \tag {9a}
y=3 \tag{10}
x+y=8 \tag{11}
\end{equation*}</text-notation="math"></disp-formula>
...etc

在文件上使用幾個正則表達式替換。 第一個正則表達式替換看起來像

(?s)(<disp-formula id="deqn)[^"]*?("(?:.(?!/disp-formula))+?.\\tag\s?\{)([^}]+?)(\}(?:.(?!/disp-formula))+.\\tag\s?\{)([^}]+?)\}

被替換為

$1$3-$5$2$3$4$5}

第二個正則表達式是

(?s)(<disp-formula id="deqn)[^"]*?("(?:.(?!/disp-formula|\\tag))+?.\\tag\s?\{)([^}]+?)(\}(?:.(?!/disp-formula|\\tag))+?</disp-formula>)

這將被替換

$1$3$2$3$4

這兩個正則表達式都已使用http://regexstorm.net/tester進行了測試,並且可以正常工作,但是當我嘗試在我的代碼中使用它時卻無法正常工作。

我想我正在努力逃避我的正則表達式中的一些字符,任何人都可以幫我這里是我的代碼

string content=File.ReadAllText(@"D:\test\00057_po.txt");
string pattern1 = "(?s)(<disp-formula id=\"deqn)[^\"]*?(\"(?:.(?!/disp-formula))+?.\\tag\\s?{{)([^}]+?)(}}(?:.(?!/disp-formula))+.\\tag\\s?{{)([^}]+?)}}";
string replacement1 = "$1$3-$5$2$3$4$5}}";
string pattern2="(?s)(<disp-formula id=\"deqn)[^\"]*?(\"(?:.(?!/disp-formula|\\tag))+?.\\tag\\s?{{)([^}]+?)(}}(?:.(?!/disp-formula|\\tag))+?</disp-formula>)";
string replacement2 = "$1$3$2$3$4";
Regex rgx = new Regex(pattern1);
Regex rgx2 = new Regex(pattern2);
string result1 = rgx.Replace(content, replacement1);
string result2 = rgx2.Replace(result1, replacement2);
File.WriteAllText(@"D:\test\00057_po.txt",result2);

試試這些

    string pattern1 = "(?s)(<disp-formula id=\"deqn)[^\"]*?(\"(?:.(?!/disp-formula))+?.\\\\tag\\s?\\{)([^\\}]+?)(\\}(?:.(?!/disp-formula))+.\\\\tag\\s?\\{)([^}]+?)(?=\\})";
    string replacement1 = "$1$3-$5$2$3$4$5";

    string pattern2="(?s)(<disp-formula id=\"deqn)[^\"]*?(\"(?:.(?!/disp-formula|\\\\tag))+?.\\\\tag\\s?\\{)([^\\}]+?)(\\}(?:.(?!/disp-formula|\\\\tag))+?</disp-formula>)";
    string replacement2 = "$1$3$2$3$4";

您還需要轉義 {},例如:

"(?+s)(<disp-formula id=\"deqn)[^\"]*?(\"(?:.(?!\\/disp-formula))+?.\\tag\\s?\\{\\{)([^\\}]+?)(\\}\\}(?:.(?!/disp-formula))+.\\tag\\s?\\{\\{)([^\\}]+?)\\}\\}";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM