[英]C# Regex Match/Replace Large Text File
我有一個大的文本文件,最大可以達到+ 500MB),我需要替換所有出現在特定字符串中的日期。 我使用正則表達式匹配日期,效果很好。 我需要捕獲行號,匹配項以及匹配項所在的整行。我正在處理該部分,這是我一直在努力的替換部分。 理想情況下,我想進行匹配,捕獲更多信息並在一遍文件中進行替換。 我如何有效地做到這一點? 這就是我用來執行正則表達式的東西。
while ((line = InputFile.ReadLine()) != null)
{
// Increment for each line read
x++;
// Try to match each line against the Regex.
Match m = reg.Match(line);
if (m.Success)
{
DateTime result;
if (!(DateTime.TryParse(m.Groups[0].Value, out result)))
{
// add it to the DT
MatchTable.Rows.Add(x, m.Groups[0].Value, line);
}
else if (DateTime.Parse(m.Groups[0].Value).Year <= 1753) // 1753 is the earliest date that can be stored in SQL datetime
{
// add it to the DT
MatchTable.Rows.Add(x, m.Groups[0].Value, line);
}
}
}
我想我可能會通過修改后的行來構建第二個臨時文件行,然后刪除舊文件並在完成后重命名新文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.