RegEx 패턴이있는 지침이 필요합니다.C# RegEx가 파이프로 구분 된 파일에서 빈 셀을 찾습니다.
나는 네 번째 셀이 비어있는 모든 줄을 제거하려고하는 파이프 구분 파일이 있습니다. 각 행에는 셀 수에 제한이 없습니다. 지금까지
내 코드 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace EpicRemoveBlankPriceRecords
{
class Program
{
static void Main(string[] args)
{
string line;
// Read the file and display it line by line.
System.IO.StreamReader inFile = new System.IO.StreamReader("c:\\test\\test.txt");
System.IO.StreamWriter outFile = new System.IO.StreamWriter("c:\\test\\test_out.txt");
while ((line = inFile.ReadLine()) != null)
{
Match myMatch = Regex.Match(line, @".*\|.*\|.*\|\|.*");
if (!myMatch.Success)
{
outFile.WriteLine(line);
}
}
inFile.Close();
outFile.Close();
//// Suspend the screen.
//Console.ReadLine();
}
}
}
이 작동하지 않습니다. RegEx가 "탐욕 스럽다"고 생각하기 때문에 - "파이프 문자를 제외한 모든 것을 잡아라"라고 명시 적으로 말하지 않았기 때문에 공백 셀이 있으면 일치합니다. 빠른 google과 나는 패턴에서 [^ \ |]을 사용하여 그것을 할 수 있음을 알았다.
그래서,에 패턴을 변경하는 경우 :하지 않는 이유는이 작품 중 하나
".*[^\|]\|.*[^\|]\|.*[^\|]\|\|.*"
?
내가 약간 혼란 스럽다는 것을 짐작할 수 있습니다.
감사합니다.
너는 나에게 너무 빨랐다. 나는 그것을 알아 차리고 그에 따라 편집했다. 불행히도 내 패턴은 여전히 작동하지 않습니다. 감사합니다. – Ekins86
정규 표현식을 사용해야하는 이유가 있습니까? 나에게'string.IsNullOrEmpty (line.Split ('|') [2])와 같은 것을하는 것이 훨씬 쉽습니다. –
1에서 1 번째 또는 0에서 3 번째 항목? =) – Maslow