2011-07-18 25 views
3

我有一些數據線..它看起來與此類似: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 

問題:

  • 是否有更簡單的方法去嗎?
  • 如果是這樣,我可以使用正則表達式來解決這個問題,還是我必須使用不同的方式?

回答

2

只是重新規劃測試你的格式的正則表達式,以匹配所有不以9開頭的東西 - 以9開頭的那些行不會被添加到富文本框中。

+0

你知道我會怎麼做嗎?我如何排除? – theNoobGuy

+0

基本上,只需用'@「^ [\。* \ d] + \ s + [\ d \ w] + \ s + [\ d \ - \ w *] + \ s +。{25}^[0-8] [\ d] {3}',以9開頭的行不再匹配。 –

1

是的,有一個更簡單的方法。只需使用Regex.Replace方法,並提供Multiline選項。

2

試試這個(使用LINQ):

//Create a regex to identify lines that start with 9XXX 
Regex rgx = new Regex(@"^9\d{3}"); 
//Below is the linq expression to filter the lines that start with 9XXX 
var validLines = 
(
//This following line specifies what enumeration to pick the data from 
from ln in File.ReadAllLines(openFile.FileName) 
//This following specifies what is the filter that needs to be applied to select the data. 
where !rgx.IsMatch(ln) 
//This following specifies what to select from the filtered data. 
select ln; 
).ToArray(); //This line makes the IQueryable enumeration to an array of Strings (since variable ln in the above expression is a String) 
//Finally join the filtered entries with a \n using String.Join and then append it to the textbox 
formattedTextRichTextBox.AppendText = String.Join(validLines, "\n"); 
+0

好的建議。基於問題和示例代碼,我會說正則表達式應該是「@」^ 9 \ d {3}「',以便它匹配一個9後跟三個數字。 –

+0

@John:Thx。更新了帖子,使正則表達式的限制更少 – Chandu

+0

@Cyber​​nate:我不確定這段代碼中發生了什麼。你能解釋一下嗎? – theNoobGuy

1

你爲什麼不只是比賽的第一部分9XXX使用通配符來該行的其餘部分相匹配,這將是一個很大的可讀性。

output = Regex.Replace(output, @"^9[\d{3}].*", "")

+0

我試過這個,出於某種原因,它不會工作。我不知道爲什麼.. – theNoobGuy