首先将该行与正则表达式匹配。然后,如果匹配成功,则输出新行。我不知道 VB.net,但 C# 中的函数类似于:
void replaceLines(string inputFilePath, string outputFilePath, string pattern, string replacement)
{
Regex regex = new Regex(pattern);
using (StreamReader reader = new StreamReader(inputFilePath))
using (StreamWriter writer = new StreamWriter(outputFilePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (regex.IsMatch(line))
{
writer.Write(replacement);
}
else
{
writer.Write(line);
}
}
}
}
然后你会这样称呼它:
replaceLines(@"C:\temp\input.txt", @"c:\temp\output.txt", "id", "The correct line");
希望这可以帮助。