0
我目前正在開發一個項目,我必須加載並過濾一個txt文件,並將過濾後的txt保存到列表中。目前,我有過濾器工作,但他們不維護.txt文件的順序。過濾List/.Txt文件
我明白爲什麼會發生這種情況,因爲try塊先於另一個塊運行,但我想知道是否有辦法讓我的代碼更加高效,並且爲了使所需的行保存爲了他們被閱讀。
我想這也可能會更好,如果方式變量保存在一個變量,但是,我不知道他們應該怎樣被格式化爲Regax.Match
當我嘗試串既結合不起作用。
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace program
{
internal class Program
{
private static void Main(string[] args)
{
var inputFile = File.ReadAllText(@"C:\olive.txt");
Filter(inputFile);
}
public static void Filter(string fileToBeFiltered)
{
var sendList = new List<string>();
Match m;
var sendPattern = @"(SEND.+)";
var getPattern = @"(GET.+)"; //+ "(SEND.+)";
try
{
m = Regex.Match(fileToBeFiltered, getPattern);
while (m.Success)
{
var add = m.Groups[1].ToString();
sendList.Add(add);
m = m.NextMatch();
}
}
catch (RegexMatchTimeoutException)
{
Console.WriteLine("The matching operation timed out.");
}
try
{
m = Regex.Match(fileToBeFiltered, sendPattern);
while (m.Success)
{
var add = m.Groups[1].ToString();
sendList.Add(add);
m = m.NextMatch();
}
}
catch (RegexMatchTimeoutException)
{
Console.WriteLine("The matching operation timed out.");
}
foreach (var value in sendList)
Console.WriteLine(value);
Console.WriteLine();
}
}
}
SO是用於調試的目的。您可以嘗試http://codereview.stackexchange.com/進行改進 –