Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 拆分字符串并将其分配给字典_C#_String_Dictionary_Split - Fatal编程技术网

C# 拆分字符串并将其分配给字典

C# 拆分字符串并将其分配给字典,c#,string,dictionary,split,C#,String,Dictionary,Split,我有一个文本文件,其中包含许多行,如下所示: 花{郁金香|太阳花|玫瑰} 性别{女性|男性} 宠物{猫|狗|兔} 我知道如何从文件中读取行,但是在以后将类别及其子项拆分并存储到字典中的最佳方法是什么?假设从包含上述所有行的字符串数组中?您可以使用: string line = reader.ReadLine(); Regex r = new Regex(@"(\w+){(\w+)}"); 现在循环此正则表达式的结果: foreach(Match m in r.Matches(line)) {

我有一个文本文件,其中包含许多行,如下所示:

花{郁金香|太阳花|玫瑰}

性别{女性|男性}

宠物{猫|狗|兔}

我知道如何从文件中读取行,但是在以后将类别及其子项拆分并存储到字典中的最佳方法是什么?假设从包含上述所有行的字符串数组中?

您可以使用:

string line = reader.ReadLine();
Regex r = new Regex(@"(\w+){(\w+)}");
现在循环此正则表达式的结果:

foreach(Match m in r.Matches(line)) {
    yourDict.Add(m.Groups[1], m.Groups[2].Split(' '));
}
您可以使用以下选项:

string line = reader.ReadLine();
Regex r = new Regex(@"(\w+){(\w+)}");
现在循环此正则表达式的结果:

foreach(Match m in r.Matches(line)) {
    yourDict.Add(m.Groups[1], m.Groups[2].Split(' '));
}

您可以在此代码上使用

string T = @"Flowers{Tulip|Sun Flower|Rose}

Gender{Female|Male}

Pets{Cat|Dog|Rabbit}";

foreach (var line in T.Split('\n'))//or while(!file.EndOfFile)
{
    var S = line.Split(new char[] { '{', '|','}' }, StringSplitOptions.RemoveEmptyEntries);
    string Key = S[0];
    MessageBox.Show(Key);//sth like this
    for (int i = 1 ; i < S.Length; i++)
    {
        string value = S[i];
        MessageBox.Show(value);//sth like this
    }
}
string T=@“花{郁金香|太阳花|玫瑰}”
性别{女性|男性}
宠物{猫|狗|兔}”;
foreach(T.Split('\n'))中的var行//或while(!file.EndOfFile)
{
var S=line.Split(新字符[]{'{'','|'','}},StringSplitOptions.RemoveEmptyEntries);
字符串键=S[0];
MessageBox.Show(Key);//像这样的东西
对于(int i=1;i
您可以在此代码上使用

string T = @"Flowers{Tulip|Sun Flower|Rose}

Gender{Female|Male}

Pets{Cat|Dog|Rabbit}";

foreach (var line in T.Split('\n'))//or while(!file.EndOfFile)
{
    var S = line.Split(new char[] { '{', '|','}' }, StringSplitOptions.RemoveEmptyEntries);
    string Key = S[0];
    MessageBox.Show(Key);//sth like this
    for (int i = 1 ; i < S.Length; i++)
    {
        string value = S[i];
        MessageBox.Show(value);//sth like this
    }
}
string T=@“花{郁金香|太阳花|玫瑰}”
性别{女性|男性}
宠物{猫|狗|兔}”;
foreach(T.Split('\n'))中的var行//或while(!file.EndOfFile)
{
var S=line.Split(新字符[]{'{'','|'','}},StringSplitOptions.RemoveEmptyEntries);
字符串键=S[0];
MessageBox.Show(Key);//像这样的东西
对于(int i=1;i
使用regexp的想法是正确的,但为了可读性,我更喜欢使用命名捕获

var regexp = new Regex(@"(?<category>\w+?)\{(?<entities>.*?)\}");
var d = new Dictionary<string, List<string>>();

// you would replace this list with the lines read from the file
var list = new string[] {"Flowers{Tulip|Sun Flower|Rose}"
                         , " Gender{Female|Male}"
                         , "Pets{Cat|Dog|Rabbit}"};

foreach (var entry in list)
{
    var mc = regexp.Matches(entry);
    foreach (Match m in mc)
    {
        d.Add(m.Groups["category"].Value
            , m.Groups["entities"].Value.Split('|').ToList());
    }
}
var regexp=new Regex(@“(?\w+?)\{(?.*?\}”);
var d=新字典();
//您将用从文件中读取的行替换此列表
var list=新字符串[]{“花{郁金香|太阳花|玫瑰}”
,“性别{女性|男性}”
,“宠物{猫|狗|兔}”;
foreach(列表中的var条目)
{
var mc=regexp.Matches(条目);
foreach(在mc中匹配m)
{
d、 添加(m.Groups[“category”]值
,m.Groups[“entities”].Value.Split(“|”).ToList();
}
}

您将获得一个以类别为键的字典,以及字符串列表中的值。使用regexp的想法是正确的,但为了可读性,我更喜欢使用命名捕获

var regexp = new Regex(@"(?<category>\w+?)\{(?<entities>.*?)\}");
var d = new Dictionary<string, List<string>>();

// you would replace this list with the lines read from the file
var list = new string[] {"Flowers{Tulip|Sun Flower|Rose}"
                         , " Gender{Female|Male}"
                         , "Pets{Cat|Dog|Rabbit}"};

foreach (var entry in list)
{
    var mc = regexp.Matches(entry);
    foreach (Match m in mc)
    {
        d.Add(m.Groups["category"].Value
            , m.Groups["entities"].Value.Split('|').ToList());
    }
}
var regexp=new Regex(@“(?\w+?)\{(?.*?\}”);
var d=新字典();
//您将用从文件中读取的行替换此列表
var list=新字符串[]{“花{郁金香|太阳花|玫瑰}”
,“性别{女性|男性}”
,“宠物{猫|狗|兔}”;
foreach(列表中的var条目)
{
var mc=regexp.Matches(条目);
foreach(在mc中匹配m)
{
d、 添加(m.Groups[“category”]值
,m.Groups[“entities”].Value.Split(“|”).ToList();
}
}

您将获得一个以类别为键的字典,以及字符串列表中的值

您希望从输出中得到什么?一个键和一些值?我想在列表视图中添加类别(如花)作为项目,当我选择类别时,我想在第二个列表视图中显示子类别(郁金香、太阳花、玫瑰)。但是知道如何正确拆分并将其放入字典将非常有帮助。“|”占位符是可能值的占位符还是文件中的实际字符?是的,它们是实际字符。我使用它们作为分隔符。您希望从输出中得到什么?一个键和一些值?我想在列表视图中添加类别(如花)作为项目,当我选择类别时,我想在第二个列表视图中显示子类别(郁金香、太阳花、玫瑰)。但是知道如何正确拆分并将其放入字典将非常有帮助。“|”占位符是可能值的占位符还是文件中的实际字符?是的,它们是实际字符。我用它们作为分隔物。谢谢,这正是我需要的!谢谢,这正是我需要的!