C#正则表达式提取键值

时间:2014-11-02 14:44:23

标签: c# regex

是否有一种简单而优雅的方法可以从以下格式的字符串中提取键值对?

"key1='value1' key2='value 2' key3='value3' key4='value4' key5='5555' key6='xxx666'"

我的尝试导致了这一点,但我对它不太满意

var regex = new Regex(@"\'\s", RegexOptions.None);
var someString = @"key1='value1' key2='value 2' key3='value3' key4='value4' key5='5555' key6='xxx666'" + " ";

var splitArray = regex.Split(someString);

IDictionary<string, string> keyValuePairs = new Dictionary<string, string>();

foreach (var split in splitArray)
{
    regex = new Regex(@"\=\'", RegexOptions.None);

    var keyValuArray = regex.Split(split);

    if (keyValuArray.Length > 1)
    {
        keyValuePairs.Add(keyValuArray[0], keyValuArray[1]);
    }

}

3 个答案:

答案 0 :(得分:4)

您应该能够在不进行拆分的情况下使用MatchCollection来代替:

var rx = new Regex("([^=\\s]+)='([^']*)'");
var str = "key1='value1' key2='value 2' key3='value3' key4='value4' key5='5555' key6='xxx666'";
foreach (Match m in rx.Matches(str)) {
    Console.WriteLine("{0} {1}", m.Groups[1], m.Groups[2]);
}

Demo.

此解决方案的核心是这个正则表达式:([^=\\s]+)='([^']*)'它定义了键值对的结构:一系列非空格字符定义键,然后是等号,后跟值用单引号括起来。此解决方案按顺序遍历匹配项,按顺序提取分配给匹配组Group[1]Group[2]的键和值。

答案 1 :(得分:1)

另一种方法:

var someString = @"key1='value1' key2='value 2' key3='value3' key4='value4' key5='5555' key6='xxx666'" + " ";
Dictionary<string, string> dic = Regex.Matches(someString, @"(?<key>\w+)='(?<value>[^']*)'")
                                      .OfType<Match>()
                                      .ToDictionary(m => m.Groups["key"].Value, m => m.Groups["value"].Value);

答案 2 :(得分:0)

你可以这样做

var str = "key1='value1' key2='value 2' key3='value3' key4='value4' key5='5555' key6='xxx666'";
var arr = Regex.Split(str, "(?<=')\\s(?=\\w)"); // split on whitespace to get key=value
foreach(var s in arr) {
    var nArr = s.Split("="); // split on = to get key and value
    keyValuePairs.Add(nArr[0], nArr[1]);
}

(?<=')\s(?=\w)会查找'之后和密钥开头之前的空格