2016-10-03 3 views
0

현재로드되고 필터링 된 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(); 
    } 
} 
} 
+0

SO는 디버깅 용도로 사용됩니다. 개선을 위해 http://codereview.stackexchange.com/을 시도해보십시오. –

답변

0

송신 패턴과 get 패턴을 별도로 검색하므로 출력 결과가 표시되지 않습니다. 당신의 패턴을 변경합니다

var pattern = @"((SEND.+)|(GET.+))"; 

이제 단일 루프 송신과 일치 또는 패턴 파일의 순서대로 목록에 추가됩니다 얻을 세그먼트를 둡니다.

편집 : 다른 쌍의 괄호가 추가되었습니다.

+0

감사합니다. 큰 도움이됩니다. –