2017-09-14 92 views
0

我有一個包含許多行的.txt文件。我試圖捕捉只有以D開頭的行。但是這裏有一個技巧:我還想用它捕捉2個進程行(當D行開始時),以便捕獲它來自哪個站點和程序。這是我所期待的嘗試從字符串中捕獲或分組txt到列表框中C#

期望的結果: [文字期望] [1]

另外,d線可以用不同長度出現在文本多次,所以不是3個系的「D」可能有1000或10個,或者你可以得到它。

我該怎麼寫這個,可以這樣做? ANEL系統名稱:BAUER節點02 程序名稱:ABU.DEH1LR.PGM 面板系統名稱:BAUER節點02 程序名稱:ABU.RT1LR.PGM 面板系統名稱:BAUER節點02 程序名稱:ABU.RT2LR。 PGM 面板系統名稱:BAUER節點02 程序名稱:ABU.RT3LR.PGM 面板系統名稱:BAUER節點03 程序名稱:ABA.LIGHTING.PGM 面板系統名稱:BAUER節點03 程序名稱:ABA.RT1LR程序名稱:ABA.RT2LR.PGM D 5205 LOOP(128%X%SAT%X%VRT%X%SAS 1000 15 0 1 0 0 100 0) D 5210 DBSWIT(0 ABA.RT2LR.HCO 2.0 8.0 ABA.RT2LR.HT1) D 5220表(ABA.RT2LR.VRT%X%HCO 0 0 100 100) 面板系統名稱:BAUER節點03 程序名稱: ABA.RT3LR.PGM 面板系統名稱:BAUER節點03 程序名稱:ABA.ZONE.VLV.PGM 面板系統名稱:BAUER節點03 程序名稱:ABU.CAR.PLUG.PGM 面板系統名稱:BAUER節點04 程序名稱:ABA.RT4LR.PGM 面板系統名稱:BAUER節點04 程序名稱:BAUERBUSH PPCL 4 面板系統名稱:BAUER節點05 程序名稱:ABA.DEH1LR.PGM 面板系統名稱:BAUER節點0 6 程序名稱:ABA.RT5LR.PGM

enter code here 
+0

我們認爲這是非常粗魯的發佈圖像,而不是喲文本我們的樣本數據。這也使我們很難幫助你(我們不能將你的數據複製/粘貼到示例程序中來測試它),這意味着你很難得到一個好的答案。 –

+0

注意到...新的在這裏..現在可以感覺到它就是這樣。謝謝你的提示。 – Metrics

+0

閱讀[本文](http://stackoverflow.com/editing-help)瞭解如何在SO中正確使用代碼塊。 –

回答

0

添加列表框(名稱:MainListBox)和按鈕(名稱:GetTextButton)到Windows窗體應用程序
並使用此代碼隱藏:

using System.Collections.Generic; 
using System.IO; 
using System.Windows.Forms; 

namespace StackAnswer 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void GetTextButton_Click(object sender, System.EventArgs e) 
     { 
      var source = File.ReadAllLines("C:\\Users\\User\\Desktop\\Test.txt"); 

      MainListBox.Items.Clear(); 

      List<string> result = new List<string>(); 
      for (int i = 0; i < source.Length; i++) 
      { 
       if (source[i].StartsWith("D") 
        && !source[i - 1].StartsWith("D")) 
       { 
        result.Add(source[i - 2]); 
        result.Add(source[i - 1]); 
        result.Add(source[i]); 
       } 
       else if (source[i].StartsWith("D")) 
       { 
        result.Add(source[i]); 
       } 
      } 
      MainListBox.Items.AddRange(result.ToArray()); 
     } 
    } 
} 

WPF版本:

<Window x:Class="StackAnswerWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:StackAnswerWPF" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="283*" /> 
      <RowDefinition Height="37*" /> 
     </Grid.RowDefinitions> 
     <ListBox Name="MainListBox" /> 
     <Button Name="GetTextButton" Grid.Row="1" Click="GetText">GetText</Button> 
    </Grid> 
</Window> 

代碼隱藏:

using System.Collections.Generic; 
using System.IO; 
using System.Windows; 

namespace StackAnswerWPF 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void GetText(object sender, RoutedEventArgs e) 
     { 
      var source = File.ReadAllLines("C:\\Users\\User\\Desktop\\Test.txt"); 

      MainListBox.ItemsSource = null; 

      List<string> result = new List<string>(); 
      for (int i = 0; i < source.Length; i++) 
      { 
       if (source[i].StartsWith("D") 
        && !source[i - 1].StartsWith("D")) 
       { 
        result.Add(source[i - 2]); 
        result.Add(source[i - 1]); 
        result.Add(source[i]); 
       } 
       else if (source[i].StartsWith("D")) 
       { 
        result.Add(source[i]); 
       } 
      } 
      MainListBox.ItemsSource = result; 
     } 
    } 
} 
+0

謝謝....對於WPF怎麼樣? – Metrics

+0

感謝您的幫助! – Metrics

0

保持了前兩行的緩衝區。

下面是被直接鍵入回覆窗口,是完全未經測試的樣本,因此可能有多個錯誤,但仍然會在概念上顯示您需要做什麼:

bool bufferready = false; 
string prev1="", prev2=""; 
StringBuilder result = new StringBuilder(); 
foreach (string line in File.ReadLines("path")) 
{ 
    if (line.StartsWith("D")) 
    { 
     if (bufferready) 
     { 
      result.AppendLine(prev1); 
      result.AppendLine(prev2); 
      prev1 = ""; 
      prev2 = ""; 
      bufferready = false; 
     } 
     result.AppendLine(line); 
    } 
    else 
    { 
     prev1 = prev2; 
     prev2 = line; 
     bufferready = true; 
    } 
} 

如果您已提供樣品數據作爲文本,而不是圖像,我會先在Visual Studio中嘗試此操作,並在發佈之前清除所有錯誤。

+0

對不起,我會上傳txt ..你的幫助表示感謝.. – Metrics