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

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

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*\}$)

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 }";
Regex regex = new Regex(@"\S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$)");
var results = regex.Matches(input);
foreach (Match match in results)
{
    Console.WriteLine(match.Groups[0].Value);
}

这将打印:

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

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

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

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

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 }";
Regex regex = new Regex(@"\S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$)");
var results = regex.Matches(input);
foreach (Match match in results)
{
    Console.WriteLine(match.Groups[0].Value);
}

This prints:

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

Here is an explanation of the regex pattern used:

\S+                        match a key
\s*                        followed by optional whitespace and
=                          literal '='
\s*                        more optional whitespace
.*?                        match anything until seeing
(?=\s*,\s*\S+\s*=|\s*\}$)  that what follows is either the start of the next key/value OR
                           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:

确定