c# - Parsing into dictionary with regex as separator for splitting -
as said in title, think idea split this\d+?=.*?\d=
not quite sure... idea how best parse string:
1=some dummy sentence 2=some other sentence 3=third sentence can in same line 4=forth sentence text shouldn't captured , spplitted
and i'm hoping dictionary have number key, , string in value, example:
1, "some dummy sentence" 2, "some other sentence" 3, "third sentence can in same line" 4, "forth sentence"
method parse text dictionary:
public static dictionary<int, string> getvaluestodictionary(string text) { var pattern = @"(\d+)=(.*?)((?=\d=)|\n)"; //if spaces between digit , equal sign possible (\d+)\s*=\s*(.*?)((?=\d\s?=)|\n) var regex = new regex(pattern); var pairs = new dictionary<int, string>(); var matches = regex.matches(text); foreach (match match in matches) { var key = int.parse(match.groups[1].value); var value = match.groups[2].value; if (!pairs.containskey(key)) { pairs.add(key, value); } //pairs.add(key, value); } return pairs; }
in case check if lkey exists , if not add can see if need check. includes digit groups without equal sign in value.
Comments
Post a Comment