PathExtensions.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace MediaBrowser.Server.Implementations.Library
  4. {
  5. public static class PathExtensions
  6. {
  7. /// <summary>
  8. /// Gets the attribute value.
  9. /// </summary>
  10. /// <param name="str">The STR.</param>
  11. /// <param name="attrib">The attrib.</param>
  12. /// <returns>System.String.</returns>
  13. /// <exception cref="System.ArgumentNullException">attrib</exception>
  14. public static string GetAttributeValue(this string str, string attrib)
  15. {
  16. if (string.IsNullOrEmpty(str))
  17. {
  18. throw new ArgumentNullException("str");
  19. }
  20. if (string.IsNullOrEmpty(attrib))
  21. {
  22. throw new ArgumentNullException("attrib");
  23. }
  24. string srch = "[" + attrib + "=";
  25. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  26. if (start > -1)
  27. {
  28. start += srch.Length;
  29. int end = str.IndexOf(']', start);
  30. return str.Substring(start, end - start);
  31. }
  32. // for imdbid we also accept pattern matching
  33. if (attrib == "imdbid")
  34. {
  35. Regex imdbPattern = new Regex("tt\\d{7}");
  36. var m = imdbPattern.Match(str);
  37. return m.Success ? m.Value : null;
  38. }
  39. return null;
  40. }
  41. }
  42. }