如何使用正则表达式将此分隔文本拆分为键和值?

Mar*_*C80 0 c# regex

我已经登陆了一列看起来像这样的数据:

1[::]One[::]2[::]Two[::]3[::]Three
Run Code Online (Sandbox Code Playgroud)

如何将其拆分为C#中的Dictionary?给:

1,"One"
2,"Two"
3,"Three"
Run Code Online (Sandbox Code Playgroud)

正则表达式是正确的方法吗?

我已经走到了这一步,\d+\[::]但我不确定下一步该做什么

Dou*_*las 7

你真的不需要正则表达式; 只是使用String.Split(String[], ...)方法拆分分隔符:

var str = "1[::]One[::]2[::]Two[::]3[::]Three";
var parts = str.Split(new [] { "[::]" }, int.MaxValue, StringSplitOptions.None);
// parts is an array that contains: "1", "One", "2", "Two", "3", "Three"

var dict = new Dictionary<int, string>();
for (int i = 0; i < parts.Length; i += 2)
    dict.Add(int.Parse(parts[i]), parts[i + 1]);
Run Code Online (Sandbox Code Playgroud)