这是我的函数,我正在尝试替换文件中的字符串,但是c#告诉我我的正则表达式格式错误.有任何想法吗?
public void function(string fileName, string path) {
string pathToAmmend = @"$SERVERROOT\pathpath";
string newPath = @"$SERVERROOT\" + path;
File.WriteAllText(fileName, Regex.Replace(File.ReadAllText(fileName), pathToAmmend, newPath));
....
}
Run Code Online (Sandbox Code Playgroud)
如果我将字符串更改为:
string pathToAmmend = @"$SERVERROOT\\pathpath";
string newPath = @"$SERVERROOT\\" + path;
Run Code Online (Sandbox Code Playgroud)
但后来我有两个斜线,我只想要一个斜线.
听起来你实际上根本不需要正则表达式.听起来你可能只是想要string.Replace:
// Split into three statements for clarity.
string input = File.ReadAllText(fileName);
string output = input.Replace(pathToAmend, newPath);
File.WriteAllText(output);
Run Code Online (Sandbox Code Playgroud)
只有在真正尝试匹配模式时才使用正则表达式.