将逗号分隔的键拆分为字符串数组,而不包括值 c#

huangapple go评论75阅读模式
英文:

Split into string array only the key by comma and not values c#

问题

这是我的字符串,当我尝试使用逗号分隔键的字符串数组时,出现问题。

> { Yr = 2019, Mth = DECEMBER , SeqN = 0, UComment = tet,tet1, OComment = test,test1, FWkMth = WK, FSafety = Y, FCustConsign = Y, FNCNRPull = 0, FNCNRPush = 0, CreatedTime = 2020-01-03 06:16:53 }

当我尝试使用 string.Split(',') 时,我得到了 "Ucomment = tet","tet1" 作为单独的数组。
但我需要在逗号分隔时得到分割的字符串数组。

> UComment = tet,tet1
> OComment = test,test1

我尝试使用正则表达式 ,(?=([^"]"[^"]")[^"]$)",但它没有起作用。

英文:

This is my String and i have problems when splitting into string array with comma seperated values for keys

> { Yr = 2019, Mth = DECEMBER , SeqN = 0, UComment = tet,tet1, OComment = test,test1, FWkMth = WK, FSafety = Y, FCustConsign = Y, FNCNRPull = 0, FNCNRPush = 0, CreatedTime = 2020-01-03 06:16:53 }

when i try to use string.Split(',') i get "Ucomment = tet","tet1" as seperate array.
But i need to have split string[] when seperated by comma

> UComment = tet,tet1
> OComment = test,test1

I have tried using the regex ,(?=([^"]"[^"]")[^"]$)" but it didnot work.

答案1

得分: 1

以下是已翻译的内容:

你可以尝试匹配正则表达式模式\S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$)

  1. string input = "{ Yr = 2019, Mth = DECEMBER , SeqN = 0, UComment = tet,tet1, OComment = test,test1, FWkMth = WK, FSafety = Y, FCustConsign = Y, FNCNRPull = 0, FNCNRPush = 0, CreatedTime = 2020-01-03 06:16:53 }";
  2. Regex regex = new Regex(@"\S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$)");
  3. var results = regex.Matches(input);
  4. foreach (Match match in results)
  5. {
  6. Console.WriteLine(match.Groups[0].Value);
  7. }

这将打印:

  1. Yr = 2019
  2. Mth = DECEMBER
  3. SeqN = 0
  4. UComment = tet,tet1
  5. OComment = test,test1
  6. FWkMth = WK
  7. FSafety = Y
  8. FCustConsign = Y
  9. FNCNRPull = 0
  10. FNCNRPush = 0
  11. CreatedTime = 2020-01-03 06:16:53

以下是正则表达式模式的解释:

  • \S+:匹配一个键(非空白字符的序列)。
  • \s*:匹配可选的空格。
  • =:匹配等号。
  • .*?:匹配任何字符,直到看到下一个键/值对的开始或输入的结束。
  • (?=\s*,\s*\S+\s*=|\s*\}$):这部分是一个正向预查,它要求接下来的内容要么是下一个键/值对的开始,要么是输入的结束。
英文:

You may try matching on the regex pattern \S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$):

  1. string input = "{ Yr = 2019, Mth = DECEMBER , SeqN = 0, UComment = tet,tet1, OComment = test,test1, FWkMth = WK, FSafety = Y, FCustConsign = Y, FNCNRPull = 0, FNCNRPush = 0, CreatedTime = 2020-01-03 06:16:53 }";
  2. Regex regex = new Regex(@"\S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$)");
  3. var results = regex.Matches(input);
  4. foreach (Match match in results)
  5. {
  6. Console.WriteLine(match.Groups[0].Value);
  7. }

This prints:

  1. Yr = 2019
  2. Mth = DECEMBER
  3. SeqN = 0
  4. UComment = tet,tet1
  5. OComment = test,test1
  6. FWkMth = WK
  7. FSafety = Y
  8. FCustConsign = Y
  9. FNCNRPull = 0
  10. FNCNRPush = 0
  11. CreatedTime = 2020-01-03 06:16:53

Here is an explanation of the regex pattern used:

  1. \S+ match a key
  2. \s* followed by optional whitespace and
  3. = literal '='
  4. \s* more optional whitespace
  5. .*? match anything until seeing
  6. (?=\s*,\s*\S+\s*=|\s*\}$) that what follows is either the start of the next key/value OR
  7. is the end of the input

huangapple
  • 本文由 发表于 2020年1月3日 14:38:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定