aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Providers/NameParser.cs
blob: cdd0974eac7ac4f822f2f2939d4c8d4841ae2800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Text.RegularExpressions;

namespace MediaBrowser.Controller.Providers
{
    public static class NameParser
    {
        static readonly Regex[] NameMatches =
        {
            new Regex(@"(?<name>.*)\((?<year>\d{4})\)"), // matches "My Movie (2001)" and gives us the name and the year
            new Regex(@"(?<name>.*)(\.(?<year>\d{4})(\.|$)).*$"), 
            new Regex(@"(?<name>.*)") // last resort matches the whole string as the name
        };

        /// <summary>
        /// Parses the name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="justName">Name of the just.</param>
        /// <param name="year">The year.</param>
        public static void ParseName(string name, out string justName, out int? year)
        {
            justName = null;
            year = null;
            foreach (var re in NameMatches)
            {
                Match m = re.Match(name);
                if (m.Success)
                {
                    justName = m.Groups["name"].Value.Trim();
                    string y = m.Groups["year"] != null ? m.Groups["year"].Value : null;
                    int temp;
                    year = Int32.TryParse(y, out temp) ? temp : (int?)null;
                    break;
                }
            }
        }
    }
}