只用逗号而不是值 c# 将键拆分为字符串数组
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
我试过使用正则表达式,(?=([^\"]\"[^\"]\")[^\" ]$)”但它没有用。
您可以尝试匹配正则表达式模式 \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+ 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
这是我的字符串,我在使用逗号分隔的键值拆分成字符串数组时遇到问题
{ 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
我试过使用正则表达式,(?=([^\"]\"[^\"]\")[^\" ]$)”但它没有用。
您可以尝试匹配正则表达式模式 \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+ 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