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:
//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, I have tried my very best but I have only been programming for two weeks.
tl;dr Would like song to pass a regex test whether it contains a featured artist or not, for example:
1 – Sebastian Ingrosso – Calling (Feat. Ryan Tedder)
and
1 – Flo Rida – Whistle
Should both pass the test.
The problem is that the “(” and “)” in your regex have meaning to the Regex engine. You should use the following code:
The Escape function will change “(Feat. )” to “\(Feat. \)”, which will ensure that you are matching the parentheses and not grouping “Feat. “.
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape.aspx