我有一个批处理文件(.bat),其内容类似于下面的
cd C:\ RunningDir \ TestResults vstest.console.exe C:\方向1 \方向2 \ ProductBin \ TestBin \ BIN \调试\ BVTTests.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings / Logger:trx vstest.console.exe C:\方向1 \方向2 \ ProductBin \ TestBin \ BIN \调试\ Functional_Tests_1.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings / Logger:trx vstest.console.exe C:\方向1 \方向2 \ ProductBin \ TestBin \ BIN \调试\ EndToEndTests_Part5.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings /记录器:TRX
现在我需要找到" BVTTests.orderedtest"并将其替换为" SmokeTests.orderedtest"并替换" Functional_Tests_1"与" Functional_Tests_3"这样替换后的文件看起来像
cd C:\ RunningDir \ TestResults vstest.console.exe C:\方向1 \方向2 \ ProductBin \ TestBin \ BIN \调试\ SmokeTests.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings / Logger:trx vstest.console.exe C:\方向1 \方向2 \ ProductBin \ TestBin \ BIN \调试\ Functional_Tests_3.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings / Logger:trx vstest.console.exe C:\方向1 \方向2 \ ProductBin \ TestBin \ BIN \调试\ EndToEndTests_Part5.orderedtest /Settings:C:\Dir1\Dir2\ProductBin\TestBin\bin\Debug\QuestCodedUI.testsettings /记录器: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
答案 0 :(得分:0)
你甚至不需要正则表达式来做你想做的事情:
<!DOCTYPE html>
<html>
<head>
<script>
window.onload = eventHandler;
function eventHandler() {
document.getElementById("myBtn").onclick = myFunction;
}
function myFunction() {
var x = document.getElementById("fname");
var y = document.getElementById("fage");
var z = document.getElementById("fcity");
var everythingClass = document.getElementsByClassName('everything')[0];
everythingClass.innerHTML = x.value + y.value + z.value;
}
</script>
</head>
<body>
<div class="everything">everything goes here
</div>
<p></p>
Enter your name: <input type="text" id="fname" >
<p></p>
Enter your age: <input type="number" id="fage" >
<p></p>
Enter your city: <input type="text" id="fcity" >
<p></p>
<button id="myBtn">Try it</button>
</body>
</html>
您是否有特定原因要使用Regex?
编辑: 您可以找到测试文件的所有出现,如下所示:
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);
然后以某种方式决定你想要替换哪些以及