private void btnParseAmazonCode_Click(object sender, RoutedEventArgs e) { //string regex = @"USD ([$\d\.]*)[\s]*Amazon.com Gift Card claim code: ([\d\w\-]*)"; string regex = @"\$([\d\.]*).([\w\d]{4}-[\w\d]{6}-[\w\d]{4})"; MatchCollection matches = Regex.Matches(txtUnparsedAmazonCodes.Text, regex); bool noValue = false; if (matches.Count < 1) { regex = @"([\w\d]{4}-[\w\d]{6}-[\w\d]{4})"; matches = Regex.Matches(txtUnparsedAmazonCodes.Text, regex); noValue = true; } StringBuilder sb = new StringBuilder(); colParsedAmazonGiftCodes.Clear(); int codeCount = 0; foreach (Match m in matches) { codeCount++; if (noValue) { colParsedAmazonGiftCodes.Add(new AmazonGiftCode(codeCount, m.Groups[1].Value, null)); } else { colParsedAmazonGiftCodes.Add(new AmazonGiftCode(codeCount, m.Groups[2].Value, decimal.Parse(m.Groups[1].Value))); } } if (colParsedAmazonGiftCodes.Count < 1) { txtUnparsedAmazonCodes.Background = System.Windows.Media.Brushes.Pink; } else { txtExpectedValue.Text = colParsedAmazonGiftCodes.Sum(p => p.Value).ToString(); if (txtExpectedValue.Text == String.Empty) { txtExpectedValue.Background = System.Windows.Media.Brushes.Pink; } else { var listDuplicateGiftCodes = colParsedAmazonGiftCodes.GroupBy(p => p.Code).Where(g => g.Count() > 1).Select(p => p.Key).ToList(); if (listDuplicateGiftCodes.Count > 0) { sb = new StringBuilder(); sb.AppendLine("Duplicate gift codes detected!"); foreach (var gc in listDuplicateGiftCodes) { sb.AppendLine(gc); } MessageBox.Show(sb.ToString()); } decimal expectedValue = 0; decimal.TryParse(txtExpectedValue.Text, out expectedValue); if (expectedValue > 0) { txtExpectedValue.Background = System.Windows.Media.Brushes.White; } else { txtExpectedValue.Background = System.Windows.Media.Brushes.Pink; } } txtUnparsedAmazonCodes.Background = System.Windows.Media.Brushes.White; } //txtExpectedValue.Focus(); //txtExpectedValue.SelectAll(); }