我有一些數據線..它看起來與此類似:C# - 刪除一個匹配的正則表達式
0423 222222 ADH, TEXTEXT
0424 1234 ADH,MORE TEXT
0425 98765 ADH, TEXT 3609
2000 98765-4 LBL,IUC,PCA,S/N
0010 99999-27 LBL,IUI,1.0x.25
9000 12345678 HERE IS MORE, TEXT
9010 123-123 SOMEMORE,TEXT1231
9100 SD178 YAYFOR, TEXT01
9999 9HEY:HOW-TO DOTHIS
而且我想刪除每個整個行與開始xxx。現在我已經嘗試使用正則表達式替換值。下面是我爲:
output = Regex.Replace(output, @"^9[\d]{3}\s+[\d*\-*\w*]+\s+[\d*\w*\-*\,*\:*\;*\.*\d*\w*]+", "");
然而,這真的很難看,它實際上不會刪除整條生產線。
CODE: 這裏是我使用的代碼段:
try
{
// Resets the formattedTextRichTextBox so multiple files aren't loaded on top of eachother.
formattedTextRichTextBox.ResetText();
foreach (string line in File.ReadAllLines(openFile.FileName))
{
// Uses regular expressions to find a line that has, digit(s), space(s), digit(s) + letter(s),
// space(s), digit(s), space(s), any character (up to 25 times).
Match theMatch = Regex.Match(line, @"^[\.*\d]+\s+[\d\w]+\s+[\d\-\w*]+\s+.{25}");
if (theMatch.Success)
{
// Stores the matched value in string output.
string output = theMatch.Value;
// Replaces the text with the required layout.
output = Regex.Replace(output, @"^[\.*\d]+\s+", "");
//output = Regex.Replace(output, @"^9[\d]{3}\s+[\d*\-*\w*]+\s+[\d*\w*\-*\,*\:*\;*\.*\d*\w*]+", "");
output = Regex.Replace(output, @"\s+", " ");
// Sets the formattedTextRichTextBox to the string output.
formattedTextRichTextBox.AppendText(output);
formattedTextRichTextBox.AppendText("\n");
}
}
}
觀察: 所以我想新的數據看起來就是這樣的格式(刪除9XXX):
0423 222222 ADH, TEXTEXT
0424 1234 ADH,MORE TEXT
0425 98765 ADH, TEXT 3609
2000 98765-4 LBL,IUC,PCA,S/N
0010 99999-27 LBL,IUI,1.0x.25
問題:
- 是否有更簡單的方法去嗎?
- 如果是這樣,我可以使用正則表達式來解決這個問題,還是我必須使用不同的方式?
你知道我會怎麼做嗎?我如何排除? – theNoobGuy
基本上,只需用'@「^ [\。* \ d] + \ s + [\ d \ w] + \ s + [\ d \ - \ w *] + \ s +。{25}^[0-8] [\ d] {3}',以9開頭的行不再匹配。 –