我有以下格式的文本,我想知道从字段创建用户对象的最佳方法是什么。
我不知道正则表达式,我正在查看csharp中的字符串方法,尤其是IndexOf和LastIndexOf,但我认为这样做太乱了,因为大约有15个字段。
我想用c sharp
做这件事一些特征:
Title: Mr Company: abc capital Address1: 42 mystery lane Zip: 112312 Country: Ireland Interest: Biking, Swimming, Hiking, Topic of Interest: Europe, Asia, Capital
答案 0 :(得分:1)
这会将数据拆分为键值对并将它们存储在字典中。您可能需要进一步修改以满足更多要求。
var dictionary = data
.Split(
new[] {"\r\n"},
StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(':'))
.ToDictionary(
k => k[0].Trim(),
v => v[1].Trim());
答案 1 :(得分: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()));
}
答案 2 :(得分:0)
我强烈建议对这些案例采用更熟悉的机智正则表达式。解析&#34;一半&#34;结构化文本非常简单,逻辑与常规exp。
代表。这个(以及其他以下只是变种,有很多方法可以根据你的需要做到这一点)
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#(以及它的regexp方言),我只是想唤醒你的兴趣......