
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Implementing a Better Tokenizer
|
573
If the current Match object is operating on a ControlID_* line with a Top attribute that
is out of the specified range, the code within the
MatchHandler callback method
returns a new modified string. Otherwise, the currently matched string is returned
unchanged. This ability allows you to override the default
Replace functionality by
replacing only that part of the
source string that meets certain criteria. The code
within this callback method gives you some idea of what can be accomplished using
this replacement technique.
To make use of this callback method, you need a way to call it from the
ComplexReplace method. First, a variable of type System.Text.RegularExpressions.
MatchEvaluator
is created. This variable (replaceCallback) is the delegate that is used
to call the
MatchHandler method:
MatchEvaluator replaceCallback = new MatchEvaluator(MatchHandler);
Finally, the Replace method is called with the reference to the MatchEvaluator dele-
gate passed in as a parameter:
string newString = RE.Replace(source, replaceCallback);
See Also
See the “.NET Framework Regular Expressions” topic in the MSDN documentation.
10.7 Implementing a Better Tokenizer
Problem
A simple method of tokenizing—or breaking up a string into its discrete elements—
was presented in Recipe ...