你能改进这个C#正则表达式代码吗?

Saq*_*qib 7 c# regex

在一个程序中,我正在读取一些数据文件,其中一部分格式化为一系列记录,每个记录都放在方括号中.每条记录都包含一个部分标题和一系列键/值对.

我最初编写代码来循环并提取值,但决定使用正则表达式可以更优雅地完成.下面是我生成的代码(我刚刚在控制台应用程序中将其破解 - 所以知道变量名称不是很好,等等.

你能建议改进吗?我觉得没有必要做两个匹配和一个子串,但无法弄清楚如何在一个大步骤中完成所有操作:

string input = "[section1 key1=value1 key2=value2][section2 key1=value1 key2=value2 key3=value3][section3 key1=value1]";

MatchCollection matches=Regex.Matches(input, @"\[[^\]]*\]");
foreach (Match match in matches)
{
    string subinput = match.Value;

    int firstSpace = subinput.IndexOf(' ');
    string section = subinput.Substring(1, firstSpace-1);
    Console.WriteLine(section);

    MatchCollection newMatches = Regex.Matches(subinput.Substring(firstSpace + 1), @"\s*(\w+)\s*=\s*(\w+)\s*");
    foreach (Match newMatch in newMatches)
    {
        Console.WriteLine("{0}={1}", newMatch.Groups[1].Value, newMatch.Groups[2].Value);
    }
}
Run Code Online (Sandbox Code Playgroud)

Jef*_*ser 7

我更喜欢命名捕获,良好的格式和清晰度:

string input = "[section1 key1=value1 key2=value2][section2 key1=value1 key2=value2 key3=value3][section3 key1=value1]";
MatchCollection matches = Regex.Matches(input, @"\[
                                                    (?<sectionName>\S+)
                                                      (\s+                                                            
                                                         (?<key>[^=]+)
                                                          =
                                                         (?<value>[^ \] ]+)                                                    
                                                      )+
                                                  ]", RegexOptions.IgnorePatternWhitespace);

foreach(Match currentMatch in matches)
{
    Console.WriteLine("Section: {0}", currentMatch.Groups["sectionName"].Value);
    CaptureCollection keys = currentMatch.Groups["key"].Captures;
    CaptureCollection values = currentMatch.Groups["value"].Captures;

    for(int i = 0; i < keys.Count; i++)
    {
        Console.WriteLine("{0}={1}", keys[i].Value, values[i].Value);           
    }
}
Run Code Online (Sandbox Code Playgroud)


pat*_*jbs 5

您应该利用这些集合来获取每个密钥.那么这样的话:

        string input = "[section1 key1=value1 key2=value2][section2 key1=value1 key2=value2 key3=value3][section3 key1=value1]";

        Regex r = new Regex(@"(\[(\S+) (\s*\w+\s*=\s*\w+\s*)*\])", RegexOptions.Compiled);

        foreach (Match m in r.Matches(input))
        {
            Console.WriteLine(m.Groups[2].Value);
            foreach (Capture c in m.Groups[3].Captures)
            {
                Console.WriteLine(c.Value);
            }
        }
Run Code Online (Sandbox Code Playgroud)

结果输出:

section1
key1=value1
key2=value2
section2
key1=value1
key2=value2
key3=value3
section3
key1=value1
Run Code Online (Sandbox Code Playgroud)