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

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -