代码之家  ›  专栏  ›  技术社区  ›  Saqib

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

  •  7
  • Saqib  · 技术社区  · 17 年前

    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);
        }
    }
    
    4 回复  |  直到 17 年前
        1
  •  7
  •   Jeff Moser    17 年前

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

    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);           
        }
    }
    
        2
  •  5
  •   patjbs    17 年前

    您应该利用这些集合来获取每个密钥。那么,大致如下:

            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);
                }
            }
    

    section1
    key1=value1
    key2=value2
    section2
    key1=value1
    key2=value2
    key3=value3
    section3
    key1=value1
    
        3
  •  2
  •   Don Kirkby    17 年前

    pattern = @"\[(\S+)(\s+([^\s=]+)=([^\s\]]+))*\]"
    

    rubular.com

        4
  •  -1
  •   JP Alioto    17 年前

    var input = "[section1 key1=value1 key2=value2][section2 key1=value1 key2=value2 key3=value3][section3 key1=value1]";
    
    var ms = Regex.Matches(input, @"section(\d+)\s*(\w+=\w+)\s*(\w+=\w+)*");
    
    foreach (Match m in ms)
    {
        Console.WriteLine("Section " + m.Groups[1].Value);
    
        for (var i = 2; i < m.Groups.Count; i++)
        {
            if( !m.Groups[i].Success ) continue;
            var kvp = m.Groups[i].Value.Split( '=' );
            Console.WriteLine( "{0}={1}", kvp[0], kvp[1] );
        }
    }