현재로드되고 필터링 된 txt가 목록에 저장되는 txt 파일을 필터링해야하는 프로젝트에서 작업 중입니다. 현재 필터는 작동하지만 .txt 파일의 순서는 유지되지 않습니다.목록/.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/을 시도해보십시오. –