dknaack dknaack - 3 months ago 21
C# Question

Regex: Extract Artist and Title from Filename

I have different filenames which contains Tracknumber, Album, Artist and Title.
They dont always have the same format. Now i want to extract things linke Artist and Title from that strings.

I created and found many implementations but i was not able to get this to work reliable for the different formats. Sometimes the information is even in the Directoryname etc.

I dont expect that one regex could solve all problems. So i found that 3 different types exists.

Sample Filenames



First: All Information inside the Filename



01. Artist - Title.m4a
01: Artist - Title.aif
01 Artist - Title.mp3
Artist - Title.mp3


Second: Information inside the Filename and Directoryname



/Artist - Album/01. Title.aif
/Artist - Album/01: Title.m4a
/Artist - Album/01 Title.mp3
/Artist - Album/Title.mp3


Third: Information inside the Filename, Directoryname and Parents Directoryname



/Artist/Album/01. Title.mp3
/Artist/Album/01: Title.m4a
/Artist/Album/01 Title.mp3
/Artist/Album/Title.aif

Answer

I did it in one Regex (well using '|')

using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] inputs = {
                "01. Artist - Title.m4a",
                "01: Artist - Title.aif",
                "01 Artist - Title.mp3",
                "Artist - Title.mp3",
                "/Artist - Album/01. Title.aif",
                "/Artist - Album/01: Title.m4a",
                "/Artist - Album/01 Title.mp3",
                "/Artist - Album/Title.mp3",
                "/Artist/Album/01. Title.mp3",
                "/Artist/Album/01: Title.m4a",
                "/Artist/Album/01 Title.mp3",
                "/Artist/Album/Title.aif"
                             };

            string pattern =
                @"^\d*[\.:]{0,1}\s*(?'artist'[^/][^-]+)\s+-\s+(?'title'[^\.]+)" +
                "|" +
                @"^/(?'artist'[^-]+)-\s+(?'album'[^/]+)/\d*[\.:]{0,1}\s*(?'title'[^\.]+)" +
                "|" +
                @"^/(?'artist'[^/]+)/(?'album'[^/]+)/\d*[\.:]{0,1}\s*(?'title'[^\.]+)";

            foreach (string input in inputs)
            {
                Match match = Regex.Match(input, pattern);
                Console.WriteLine("Artist : {0}, Album : {1}, Title : {2}",
                    match.Groups["artist"].Value.Trim(),
                    match.Groups["album"].Value.Trim(),
                    match.Groups["title"].Value.Trim()
                );
            }
            Console.ReadLine();
        }
     }
}