我想解析一個在文本中生成的日誌文件。我想要檢索姓名和電話號碼。 有時信息不存在,應該是空白的。取數據解析文本文件
我見過的所有例子告訴我如何得到一個字符串的字符串或結束的開始,但不是裏面有什麼。
上面有一個示例代碼我一直與
{
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] = 「張三」
需要知道文件的格式?是csv嗎?你能發表一些例子嗎? – Zeph
請顯示一些輸入/輸出 – Sybren
也許這不是在事件halder方法中解析文件的最佳想法。如果可能的話,你可能想要異步做到這一點。 – Mithrandir