If you know that your regex will always have two capturing groups, then you can regex the regex, so to speak.
private Regex captures = new Regex(@"\(.+?\)");
public string ConstructFolderName(string firstGroup, string secondGroup, string pattern)
{
MatchCollection matches = captures.Matches(pattern);
return pattern.Replace(matches[0].Value, firstGroup).Replace(matches[1].Value, secondGroup);
}
Obviously this doesn't have any error checking and might be better done with some other method than String.Replace; but, this certainly works and should give you some ideas.
EDIT: An obvious improvement would be to actually use the pattern to validate the firstGroup and secondGroup strings before you construct them. The MatchCollection's 0 and 1 item could create their own Regex and perform a match there. I can add that if you want.
EDIT2: Here's the validation I was talking about:
private Regex captures = new Regex(@"\(.+?\)");
public string ConstructFolderName(string firstGroup, string secondGroup, string pattern)
{
MatchCollection matches = captures.Matches(pattern);
Regex firstCapture = new Regex(matches[0].Value);
if (!firstCapture.IsMatch(firstGroup))
throw new FormatException("firstGroup");
Regex secondCapture = new Regex(matches[1].Value);
if (!secondCapture.IsMatch(secondGroup))
throw new FormatException("secondGroup");
return pattern.Replace(firstCapture.ToString(), firstGroup).Replace(secondCapture.ToString(), secondGroup);
}
Also, I might add that you can change the second capturing group to ([0-9ABa-c]+) since A to B isn't really a range.