user1462199
user1462199

Reputation:

RegEx For Validating Media Files

I have some MP3 files that are named with a particular syntax, for example:

1 - Sebastian Ingrosso - Calling (Feat. Ryan Tedder)

I have written a small program in C# that reads the Track, Artist and Title from the ID3 tags. What i would like to do is write a regex expression that can validate that the files are in fact named with the syntax listed above.

So i have a class called song:

class Song
{
    //Fields
    private string _filename;
    private string _title;
    private string _artist;


    //Properties
    public string Filename
    {
        get { return _filename;  }
        set { _filename = value; }
    }

    public string Title
    {
        get { return _title;  }
        set { _title = value; }
    }

    public string Artist
    {
        get { return _artist;  }
        set { _artist = value; }
    }


    //Methods

    public bool Parse(string strfile)
    {
        bool CheckFile;

        Tags.ID3.ID3v1 Song = new Tags.ID3.ID3v1(strfile, true);
        Filename = Song.FileName;
        Title = Song.Title;
        Artist = Song.Artist;


        //Check File Name Formatting
        string RegexBuilder = @"\d\s-\s" + Artist + @"\s-\s" + Title;
        if (Regex.IsMatch(Filename, RegexBuilder))
        {
            CheckFile = true;
        }
        else
        {
            CheckFile = false;
        }
        return CheckFile;
     }

So it works, MOST OF THE TIME. The minute i have a (Feat. ) in the title it fails. The closest i could come up with is:

\d\s-\s\Artist\s-\s.*

That's obviously not going to work as any text would pass the test. Thanks in advance.

Upvotes: 0

Views: 374

Answers (1)

dantuch
dantuch

Reputation: 9293

\d\s-\s\Sebastian Ingrosso\s-\sCalling(\s\(Feat.*)?

This works for both:

1 - Sebastian Ingrosso - Calling (Feat. Ryan Tedder)
1 - Sebastian Ingrosso - Calling

So:

string RegexBuilder = @"\d\s-\s" + Artist + @"\s-\s" + Title + "(\s\(Feat.*)?";

Should do the job

It can be tried out here: http://gskinner.com/RegExr/

Upvotes: 1

Related Questions