当键本身是分隔符时,如何从字符串中提取键值对? [英] How to extract Key-Value pairs from a string, when the Key itself is the separator?

查看:134
本文介绍了当键本身是分隔符时,如何从字符串中提取键值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设宽松的格式"中有一个字符串,

Say there is a string in the loose "format",

string str = "V1,B=V1,C=V1,V2,V3,D=V1,V2,A=V1,=V2,V3";

和一组已知的密钥

List<string> lst = new List<string>() { "A", "B", "C", "D" };

如何提取以下所示的键值对? (第一个键之前的任何文本都应视为空键的值.此外,下面显示的值也删除了所有后缀逗号.)

How can the Key-Value pairs shown below be extracted? (Any text before the first Key should be treated as the Value for the null Key. Also the Values shown below have any trailing comma removed.)

Key     Value
(null)  V1
A       V1,=V2,V3     (The = here is, unfortunately, part of the value)
B       V1
C       V1,V2,V3 
D       V1,V2

此问题很困难,因为 不可能在=,上立即拆分.

This problem is difficult because it is not possible to split immediately on either the = or ,.

推荐答案

忽略已知的一组键,并假设每个键仅出现一次:

Ignoring the known set of keys, and assuming each key appears only once:

string str = "V1,B=V1,C=V1,V2,V3,D=V1,V2,A=V1,=V2,V3";

var splitByEqual = new[] {'='};

var values = Regex.Split(str, @",(?=\w+=)")
    .Select(token => token.Split(splitByEqual, 2))
    .ToDictionary(pair => pair.Length == 1 ? "" : pair.First(),
                  pair => pair.Last());

  • 正则表达式为非常简单:用逗号分隔,后跟一个密钥(任何字母数字)和等号. (如果我们允许A=V1,V2=V3,则将无效)
  • 现在我们有了集合{V1B=V1C=V1,V2,V3D=V1,V2A=V1,=V2,V3}.我们将其除以=,但不超过一次.
  • 接下来,我们创建一个字典.这条线有点难看,但并不是太重要-我们已经有了所需的数据.我还使用了空字符串而不是null.
    • The regex is pretty simple: split by commas that are followed by a key (any alphanumeric) and an equal sign. (If we allow A=V1,V2=V3 this wouldn't work)
    • Now we have the collection {V1,B=V1,C=V1,V2,V3,D=V1,V2,A=V1,=V2,V3}. We split that by =, but not more than once.
    • Next we create a dictionary. This line is a little ugly, but isn't too important - we already have the data we need. I'm also using an empty string instead of null.
    • 如果我们确实想使用已知的键列表,可以将模式更改为:

      If we do want to use the known list of keys, we can change the pattern to:

      var splitPattern = @",(?=(?:" + String.Join("|", keys.Select(Regex.Escape))) + ")=)";
      

      并使用Regex.Split(str, splitPattern).

      这篇关于当键本身是分隔符时,如何从字符串中提取键值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆