2012-07-19 38 views
1

我有以下格式的文本,我想知道什麼是最好的方法可能是從它創建一個用戶對象作爲其屬性的字段。解析文本到鍵/值對或JSON

我不知道正則表達式,以及我正在查看csharp中的字符串方法,特別是IndexOf和LastIndexOf,但我認爲這會太麻煩,因爲有大約15個字段。

我試圖做到這一點在C尖銳

一些特徵:

  1. 的鍵/場是固定的,預先知道的,所以我知道我必須尋找的東西像頭銜,公司等
  2. 地址部分是單值並遵循有一些多值字段
  3. 的多值字段可以/ maynot用逗號結束(,)
  4. 有領域如之間的一個或兩個線路剎車「國家」之後是2個線剎車,我們遇到的「利息」
 
    Title: Mr 
    Company: abc capital 
    Address1: 42 mystery lane 
    Zip: 112312 
    Country: Ireland 
    Interest: Biking, Swimming, Hiking, 
    Topic of Interest: Europe, Asia, Capital 
+0

這似乎可能是缺少'作業'標籤? – 2012-07-19 20:01:55

+0

我希望這是家庭作業,我的公司從他們的網站在電子郵件中獲取這些數據,並手動輸入到數據庫中,談論生產力 – 2012-07-19 20:08:53

回答

0

我可能會像這樣的東西去:

private Dictionary<string, IEnumerable<string>> ParseValues(string providedValues) 
    { 
     Dictionary<string, IEnumerable<string>> parsedValues = new Dictionary<string, IEnumerable<string>>(); 

     string[] lines = providedValues.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); //Your newline character here might differ, being '\r', '\n', '\r\n'... 

     foreach (string line in lines) 
     { 
      string[] lineSplit = line.Split(':'); 
      string key = lineSplit[0].Trim(); 
      IEnumerable<string> values = lineSplit[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()); //Removing empty entries here will ensure you don't get an empty for the "Interest" line, where you have 'Hiking' followed by a comma, followed by nothing else 
      parsedValues.Add(key, values); 
     } 

     return parsedValues; 
    } 

,或者如果您訂閱的概念,可讀性和可維護性並不像電話的一個偉大的大鏈冷靜:

private static Dictionary<string, IEnumerable<string>> ParseValues(string providedValues) 
    { 
     return providedValues.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(x => x.Split(':')).ToDictionary(key => key[0].Trim(), value => value[1].Split(new char[]{ ','}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim())); 
    } 
+0

這個工程,謝謝,雖然你忘了添加鍵,值對字典parsedValues – 2012-07-19 21:35:48

+0

哈!當然,謝謝。固定! – 2012-07-19 23:13:04

1

這將向上分割數據到鍵值對,並將其存儲在一個字典中前。您可能需要進一步修改以獲取更多要求。

var dictionary = data 
     .Split(
      new[] {"\r\n"}, 
      StringSplitOptions.RemoveEmptyEntries) 
     .Select(x => x.Split(':')) 
     .ToDictionary(
      k => k[0].Trim(), 
      v => v[1].Trim()); 
+0

我試了一下,有什麼不對勁,這是開始傳入你的代碼的字符串 「名稱:先生\ n公司:Nappertandy資本\ nAddress1:4 Willbrook別墅\ nAddress2:\ nCity:都柏林\ NSTATE:N/A \ nProvince:\ nZip:14 \ nCountry:愛爾蘭」 字典想出了只有一個鍵入爲「標題」,值爲「Mr \ nCompany」 – 2012-07-19 21:17:53

0

我強烈推薦在這些情況下使用更多的機智正則表達式。解析「半」結構化文本非常簡單,並且具有常規exp的邏輯。

for ex。這(和其他以下只是變種有許多方面,它取決於你需要做什麼)

title:\s*(.*)\s+comp.*?:\s*(.*)\s+addr.*?:\s*(.*)\s+zip:\s*(.*)\s+country:\s*(.*)\s+inter.*?:\s*(.*)\s+topic.*?:\s*(.*) 

給出結果

1. Mr 
2. abc capital 
3. 42 mystery lane 
4. 112312 
5. Ireland 
6. Biking, Swimming, Hiking, 
7. Europe, Asia, Capital 

或 - 更加開放的東西:

\s(.*?):\s(.*) 

將您的輸入解析爲如下所示的好羣組:

Match 1 
1. Title 
2. Mr 
Match 2 
1. Company 
2. abc capital 
Match 3 
1. Address1 
2. 42 mystery lane 
Match 4 
1. Zip 
2. 112312 
Match 5 
1. Country 
2. Ireland 
Match 6 
1. Interest 
2. Biking, Swimming, Hiking, 
Match 7 
1. Topic of Interest 
2. Europe, Asia, Capital 

我不熟悉c#(及其正則表達式的方言),我只是想喚醒你的興趣...