使用 C# 替换文件完整路径中的文件名的正则表达式
Regular expression to replace a file name in the full path to file using C#
我有一个批处理文件 (.bat),其内容如下所示
cd C:\RunningDir\TestResults vstest.console.exe
C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\BVTTests.orderedtest
/Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings
/Logger:trx vstest.console.exe
C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\Functional_Tests_1.orderedtest
/Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings
/Logger:trx vstest.console.exe
C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\EndToEndTests_Part5.orderedtest
/Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings
/Logger:trx
现在我需要找到 "BVTTests.orderedtest" 并将其替换为 "SmokeTests.orderedtest" 并将 "Functional_Tests_1" 替换为 "Functional_Tests_3" 以便替换后的文件看起来像
cd C:\RunningDir\TestResults vstest.console.exe
C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\SmokeTests.orderedtest
/Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings
/Logger:trx vstest.console.exe
C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\Functional_Tests_3.orderedtest
/Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings
/Logger:trx vstest.console.exe
C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\EndToEndTests_Part5.orderedtest
/Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings
/Logger:trx
我编写了类似下面的代码,但它用替换字符串替换了 .....\ 之间的所有字符串。请用正确的正则表达式帮助我。
var batchReader = new StreamReader(File.OpenRead(@"C:\SystemSetup\Runner.bat"));
string linevalue = String.Empty;
string pattern = @"([^\/]*.)";
Regex rgx = new Regex(pattern);
string replacement = "SmokeTests.orderedtest";
while(!batchReader.EndOfStream)
{
linevalue = batchReader.ReadLine();
if(linevalue.Contains("vstest"))
{
string newone = rgx.Replace(linevalue, replacement);
}
}
但输出是 SmokeTests.orderedtestSmokeTests.orderedtestSmokeTests.orderedtest
你甚至不需要正则表达式来做你想做的事:
var path = @"C:\SystemSetup\Runner.bat";
var text = System.IO.File.ReadAllText(path);
text = text.Replace("BVTTests.orderedtest", "SmokeTests.orderedtest");
text = text.Replace("Functional_Tests_1", "Functional_Tests_3");
System.IO.File.WriteAllText(path, text);
您想使用 Regex 有什么具体原因吗?
编辑:
您可以像这样找到所有出现的测试文件:
var matches = System.Text.RegularExpressions.Regex.Matches(text, @"(?<=\)[^\.]+?.orderedtest")
.OfType<System.Text.RegularExpressions.Match>()
.Select(m => m.Value)
.ToList();
然后以某种方式决定要替换其中的哪些以及用什么
我有一个批处理文件 (.bat),其内容如下所示
cd C:\RunningDir\TestResults vstest.console.exe C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\BVTTests.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings /Logger:trx vstest.console.exe C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\Functional_Tests_1.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings /Logger:trx vstest.console.exe C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\EndToEndTests_Part5.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings /Logger:trx
现在我需要找到 "BVTTests.orderedtest" 并将其替换为 "SmokeTests.orderedtest" 并将 "Functional_Tests_1" 替换为 "Functional_Tests_3" 以便替换后的文件看起来像
cd C:\RunningDir\TestResults vstest.console.exe C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\SmokeTests.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings /Logger:trx vstest.console.exe C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\Functional_Tests_3.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings /Logger:trx vstest.console.exe C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\EndToEndTests_Part5.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings /Logger:trx
我编写了类似下面的代码,但它用替换字符串替换了 .....\ 之间的所有字符串。请用正确的正则表达式帮助我。
var batchReader = new StreamReader(File.OpenRead(@"C:\SystemSetup\Runner.bat"));
string linevalue = String.Empty;
string pattern = @"([^\/]*.)";
Regex rgx = new Regex(pattern);
string replacement = "SmokeTests.orderedtest";
while(!batchReader.EndOfStream)
{
linevalue = batchReader.ReadLine();
if(linevalue.Contains("vstest"))
{
string newone = rgx.Replace(linevalue, replacement);
}
}
但输出是 SmokeTests.orderedtestSmokeTests.orderedtestSmokeTests.orderedtest
你甚至不需要正则表达式来做你想做的事:
var path = @"C:\SystemSetup\Runner.bat";
var text = System.IO.File.ReadAllText(path);
text = text.Replace("BVTTests.orderedtest", "SmokeTests.orderedtest");
text = text.Replace("Functional_Tests_1", "Functional_Tests_3");
System.IO.File.WriteAllText(path, text);
您想使用 Regex 有什么具体原因吗?
编辑: 您可以像这样找到所有出现的测试文件:
var matches = System.Text.RegularExpressions.Regex.Matches(text, @"(?<=\)[^\.]+?.orderedtest")
.OfType<System.Text.RegularExpressions.Match>()
.Select(m => m.Value)
.ToList();
然后以某种方式决定要替换其中的哪些以及用什么