更新时间:2023-02-26 09:16:01
忽略已知的一组键,并假设每个键仅出现一次:
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
,则将无效)V1
,B=V1
,C=V1,V2,V3
,D=V1,V2
,A=V1,=V2,V3
}.我们将其除以=
,但不超过一次.
A=V1,V2=V3
this wouldn't work)V1
,B=V1
,C=V1,V2,V3
,D=V1,V2
,A=V1,=V2,V3
}. We split that by =
, but not more than once.如果我们确实想使用已知的键列表,可以将模式更改为:
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)
.