teleias

regex 4

Aug 5th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1.     DmgProb[] regex3(string input = "0[18/6]2[20/5]10[15/4]")
  2.     {
  3.         string lastIndexPattern = @"(?<index>\d+)(\[(\d+)/(\d+)\])$";
  4.         int lastIndex = int.Parse(Regex.Match(input, lastIndexPattern).Groups["index"].Value);
  5.         string pattern = @"(?<index>\d+)\[(?<damage>\d+)/(?<probability>\d+)\]";
  6.         DmgProb[] arr = new DmgProb[lastIndex];
  7.         List<DmgProb> matches = new List<DmgProb>();
  8.         Dictionary<int, DmgProb> indexedDictionary = new Dictionary<int, DmgProb>();
  9.         foreach(Match m in Regex.Matches(input, pattern))
  10.         {
  11.             indexedDictionary.Add(
  12.                 int.Parse(m.Groups["index"].Value),
  13.                 new DmgProb{
  14.                     damage      = float.Parse(m.Groups["damage"]     .Value),
  15.                     probability = float.Parse(m.Groups["probability"].Value)
  16.                 }
  17.                 );
  18.         }
  19.         DmgProb lastDmgProb = indexedDictionary[0];
  20.         for(int i = 0; i < lastIndex; i++)
  21.         {
  22.             if(!indexedDictionary.ContainsKey(i))
  23.                 arr[i] = lastDmgProb;
  24.             else
  25.                 arr[i] = lastDmgProb = indexedDictionary[i];
  26.         }
  27.         return arr;
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment