I am using Regex with a MatchEvaluator delegate to handle a format string, for example. "Time:% t, bytes:% b" will replace "% t" with a timestamp, and "% b" with the number of bytes. Needless to say, there are many other options!
For 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. }
Which would be nice if I could use the name of the Regex group (or even the number, I'm not that fussy) in the deletion instead of comparing it with a raw match to find out what matches this:
string ReplaceMatch(Match m) { ... case "%t": ... case "%b"; ... }
It is pretty ugly; I would like to use
string ReplaceMatch(Match m) { ... case "BytesCompleted": ... case "TimeStamp": ... }
I do not see anything obvious through the debugger or through google. Any ideas?
source share