NameParser.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace MediaBrowser.Controller.Providers
  4. {
  5. public static class NameParser
  6. {
  7. static readonly Regex[] NameMatches =
  8. {
  9. new Regex(@"(?<name>.*)\((?<year>\d{4})\)"), // matches "My Movie (2001)" and gives us the name and the year
  10. new Regex(@"(?<name>.*)(\.(?<year>\d{4})(\.|$)).*$"),
  11. new Regex(@"(?<name>.*)") // last resort matches the whole string as the name
  12. };
  13. /// <summary>
  14. /// Parses the name.
  15. /// </summary>
  16. /// <param name="name">The name.</param>
  17. /// <param name="justName">Name of the just.</param>
  18. /// <param name="year">The year.</param>
  19. public static void ParseName(string name, out string justName, out int? year)
  20. {
  21. justName = null;
  22. year = null;
  23. foreach (var re in NameMatches)
  24. {
  25. Match m = re.Match(name);
  26. if (m.Success)
  27. {
  28. justName = m.Groups["name"].Value.Trim();
  29. string y = m.Groups["year"] != null ? m.Groups["year"].Value : null;
  30. int temp;
  31. year = Int32.TryParse(y, out temp) ? temp : (int?)null;
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. }