public static string ParseWildcards(this DatabaseTable movie, string pattern)
{
Dictionary<string, string> mapping = getVariableMapping(movie);
// initialize our new filename and perform regex lookup to locate variable patterns
StringBuilder newFileName = new StringBuilder(pattern);
Regex variableRegex = new Regex(@"\$\{(?<item>[^}]+)\}");
MatchCollection matches = variableRegex.Matches(pattern);
// loop through all variables and replace with actual value from movie object
int replacementOffset = 0;
foreach (System.Text.RegularExpressions.Match currMatch in matches) {
// remove the current variable from the new filename
newFileName.Remove(currMatch.Index + replacementOffset, currMatch.Length);
// try to find a value for the variable
string value;
bool found = mapping.TryGetValue(currMatch.Groups["item"].Value, out value);
// if there is no variable for what was passed move on to the next variable
if (!found) {
replacementOffset -= currMatch.Length;
continue;
}
// insert value of variable that was matched and store the offset
newFileName.Insert(currMatch.Index + replacementOffset, value);
replacementOffset = replacementOffset - currMatch.Length + value.Length;
}
return newFileName.ToString();
}