fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class Test
  8. {
  9. public class Values
  10. {
  11. public string UnitType { get; set; }
  12. public string UnitId { get; set; }
  13. public string UnitTitle { get; set; }
  14. public string Applicable { get; set; }
  15. public string DeclineSender { get; set; }
  16. public string DeclineReason { get; set; }
  17. public string TriggeredByUser { get; set; }
  18. public string DisplayReason { get; set; }
  19. public string ModifiedBy { get; set; }
  20.  
  21. public override string ToString()
  22. {
  23. return $"UnitType: {UnitType}\nUnitId: {UnitId}\nUnitTitle: {UnitTitle}\nApplicable: {Applicable}\nDeclineSender: {DeclineSender}\nDeclineReason: {DeclineReason}\nTriggeredByUser: {TriggeredByUser}\nDisplayReason: {DisplayReason}\nModifiedBy: {ModifiedBy}";
  24. }
  25.  
  26. }
  27.  
  28. public static void Main()
  29. {
  30. var pattern = @"\b(\w+(?:\s+\w+)*):\s*(.*?)(?=\s*;\w+(?:\s+\w+)*:|$)";
  31. var text = "unitType:unit_failure;unitId:b7eb;unitTitle:L1-O VEN_ACC_SETTINGS>(30s)>EXCL(A);applicable:true;comment:Decline sender:SELLER;Decline reason:VEN_ACC_SETTINGS;triggered by user:495259708;Display reason: CON_FAILURE;modified by: log_res_mon";
  32. text = Regex.Replace(text, @"(?<![^;])comment:(?=(?:unitType|unitId|unitTitle|applicable|Decline sender|Decline reason|triggered by user|Display reason|modified by):)", string.Empty, RegexOptions.IgnoreCase);
  33. var vals = new List<Values>();
  34. var v = new Values();
  35. foreach (Match m in Regex.Matches(text, pattern))
  36. {
  37. if (m.Groups[1].Value == "unitType") v.UnitType=m.Groups[2].Value;
  38. if (m.Groups[1].Value == "unitId") v.UnitId=m.Groups[2].Value;
  39. if (m.Groups[1].Value == "unitTitle") v.UnitTitle=m.Groups[2].Value;
  40. if (m.Groups[1].Value == "applicable") v.Applicable=m.Groups[2].Value;
  41. if (m.Groups[1].Value == "Decline sender") v.DeclineSender=m.Groups[2].Value;
  42. if (m.Groups[1].Value == "Decline reason") v.DeclineReason=m.Groups[2].Value;
  43. if (m.Groups[1].Value == "triggered by user") v.TriggeredByUser=m.Groups[2].Value;
  44. if (m.Groups[1].Value == "Display reason") v.DisplayReason=m.Groups[2].Value;
  45. if (m.Groups[1].Value == "modified by") v.ModifiedBy=m.Groups[2].Value;
  46.  
  47. }
  48. Console.WriteLine(v.ToString());
  49. }
  50. }
Success #stdin #stdout 0.07s 28004KB
stdin
Standard input is empty
stdout
UnitType: unit_failure
UnitId: b7eb
UnitTitle: L1-O VEN_ACC_SETTINGS>(30s)>EXCL(A)
Applicable: true
DeclineSender: SELLER
DeclineReason: VEN_ACC_SETTINGS
TriggeredByUser: 495259708
DisplayReason: CON_FAILURE
ModifiedBy: log_res_mon