[英]How to escape special characters in a regex replace in c#?
I have a text file which contains string like我有一个包含字符串的文本文件
<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
I'm trying to convert them to我正在尝试将它们转换为
<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
using a couple of regex replace on the file.在文件上使用几个正则表达式替换。 The first regex replace looks like
第一个正则表达式替换看起来像
(?s)(<disp-formula id="deqn)[^"]*?("(?:.(?!/disp-formula))+?.\\tag\s?\{)([^}]+?)(\}(?:.(?!/disp-formula))+.\\tag\s?\{)([^}]+?)\}
which is replaced by被替换为
$1$3-$5$2$3$4$5}
and the second regex is第二个正则表达式是
(?s)(<disp-formula id="deqn)[^"]*?("(?:.(?!/disp-formula|\\tag))+?.\\tag\s?\{)([^}]+?)(\}(?:.(?!/disp-formula|\\tag))+?</disp-formula>)
which will be replace by这将被替换
$1$3$2$3$4
Both the regex have been tested using http://regexstorm.net/tester and it works but when I try to use it in my code it does not work.这两个正则表达式都已使用http://regexstorm.net/tester进行了测试,并且可以正常工作,但是当我尝试在我的代码中使用它时却无法正常工作。
I'm struggling to escape some characters in my regex I think, can anyone help me here is my code我想我正在努力逃避我的正则表达式中的一些字符,任何人都可以帮我这里是我的代码
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);
Try these试试这些
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.