I am using a Regex with a MatchEvaluator delegate to process a format string, e.g. "Time: %t, bytes: %b" would replace the "%t" with a time stamp, and the "%b" with a bytes count. Needless to say, there are loads of other options!
To do this, I use:
Regex regex = new Regex("%((?<BytesCompleted>b)|(?<BytesRemaining>B)|(?<TimeStamp>t))");
string s = "%bhello%t(HH:MM:SS)%P";
string r = regex.Replace(s, new MatchEvaluator(ReplaceMatch));
and
string ReplaceMatch(Match m)
{
... Handle the match replacement.
}
What would be nice is if I could use the Regex group name (or even the number, I'm not that fussy) in the delegate instead of comparing against the raw match to find out which match this is:
string ReplaceMatch(Match m)
{
...
case "%t":
...
case "%b";
...
}
Is pretty ugly; I would like to use
string ReplaceMatch(Match m)
{
...
case "BytesCompleted":
...
case "TimeStamp":
...
}
I can't see anything obvious via the debugger, or via google. Any ideas?