2015-09-18 13 views
-1

我想解析一個在文本中生成的日誌文件。我想要檢索姓名和電話號碼。 有時信息不存在,應該是空白的。取數據解析文本文件

我見過的所有例子告訴我如何得到一個字符串的字符串或結束的開始,但不是裏面有什麼。

上面有一個示例代碼我一直與

{ 
     public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

     using(TextReader reader = new StreamReader("c:/ctb.txt")) 
     { 
      string line;      
      while ((line = reader.ReadLine()) != null) 
      { 
       // Get the contents of 'per_full_name' [str] = "Smith, John" 
       // which would be Smith, John 
       // Get the contents of 'per_phone' [str] = "1 555 555-8888" 
       // which would be 1 555 555-8888 
       // Note this exists atleast twice in the file, I only need 
       // Once into a string to be able to make textBox. 
       // Text values change. 
      } 
     } 
    } 
    } 

示例文本文件中的數據:

它是一個文本文件,格式是未知的,我beleive它的一個java輸出爲自己的日誌文件。

[str] = "BI Shared Expense" 'org_level4_name' [str] = "Business  International Ins" 'org_level4_oid' [str] = "Business" 'per_first_name' [str] = "" 'per_full_name' [str] = "Smith, John" 'per_last_name' [str] = "" 'per_middle_name' str] = "" 'per_phone' [str] = "1 555 555-8888" 'qpriority' [str] = "norm" 

非常長的文本行時,沒有wordwrapped。

我想要的輸出是字符串的全名。能夠在其他功能中使用。

IE:

  string fullname = "Smith, John"; 

現在正在嘗試此代碼。

private void button1_Click(object sender, EventArgs e) 
     { 
     using (StreamReader sr = new StreamReader("C:/ctb.txt")) 
     { 

      String line = sr.ReadToEnd(); 
      foreach (Match m in Regex.Matches(line, "/\\[(str)\\]\\ = \"\\w+\\, +\\w+\"/g")) 
      { 
       textBox1.Text = m.Value; 
       richTextBox1.Text = textBox1.Text; 
      } 
     } 
    } 

使用此頁面獲取字符串,但它希望在實際程序中進行轉義。 http://www.regexr.com/3bqll

此只查找 'per_full_name'[STR] = 「張三」

+3

需要知道文件的格式?是csv嗎?你能發表一些例子嗎? – Zeph

+2

請顯示一些輸入/輸出 – Sybren

+0

也許這不是在事件halder方法中解析文件的最佳想法。如果可能的話,你可能想要異步做到這一點。 – Mithrandir

回答

1

我會建議使用正則表達式specally與電話號碼。 C#有一個名爲Regex的類,它提供了搜索特定和動態字符串的函數。

此網站將幫助您建立你的正則表達式。 http://www.rexegg.com/regex-quickstart.html

爲例如果youre PHONENUMBER語法如下: 1 555 555-8888 可以使用此: \ d {1} \ S \ d {3} \ S \ d {3} - \ d { 4}或者\ d {2,}( - \ s)\ d {}

+0

根據日誌文件的格式可能會有點矯枉過正。 – Tbid

0

我最終與以下,感謝您的建議,以查找正則表達式。

private void button1_Click(object sender, EventArgs e) 
    {    
     using (StreamReader sr = new StreamReader("C:/ctb.txt")) 
     { 

      String line = sr.ReadToEnd(); 
      foreach (Match m in Regex.Matches(line, "\\[(str)\\]\\ = \"\\w+\\, +\\w+\"")) 
      { 
       string name = m.Value.Replace("[str] = \"", ""); 
       name = name.Replace("\"", ""); 
       textBox1.Text = name; 
      } 

     } 
     using (StreamReader sr2 = new StreamReader("C:/contacttoolbar.txt")) 
     { 
      String line = sr2.ReadToEnd(); 
      foreach (Match m2 in Regex.Matches(line, "\\[(str)\\]\\ = \"\\d\\s\\d{3}\\s\\d{3}-\\d{4}\"")) 
      { 
       string tele = m2.Value.Replace("[str] = \"", ""); 
       tele = tele.Replace("\"", ""); 
       textBox2.Text = tele; 
      } 
     } 
    } 

這將最初定位於正則表達式搜索 - [STR] =「張三」 然後我分析下來,除去STR部分,然後「末使輸出,我想。 。

導致史密斯,約翰

這同樣適用於所述電話號碼。

導致1 555 555-5555

我知道它的兩個讀取文件,但它的一個小文件。相對。