The callback method for the
Replace
method is shown here:
public static void ComplexReplace(string matchPattern, string source)
{
MatchEvaluator replaceCallback = new MatchEvaluator(MatchHandler);
Regex RE = new Regex(matchPattern, RegexOptions.Multiline);
string newString = RE.Replace(source, replaceCallback);
Console.WriteLine("Replaced String = " + newString);
}
To use this callback method with the static
Replace
method, modify the previous
ComplexReplace
method as
follows
:
public void ComplexReplace(string matchPattern, string source)
{
MatchEvaluator replaceCallback = new MatchEvaluator(MatchHandler);
string newString = Regex.Replace(source, matchPattern,
replaceCallback);
Console.WriteLine("Replaced String = " + newString);
}
where
source
is the original
string to run the replace operation against, and
matchPattern
is the regular expression pattern to
match in the
source
string.
If the
ComplexReplace
method is called
from the following code:
public static void TestComplexReplace( )
{
string matchPattern = "(ControlID_.*)";
string source = @"WindowID=Main
ControlID_TextBox1 Top=-100 Left=0 Text=BLANK
ControlID_Label1 Top=9999990 Left=0 Caption=Enter Name Here
ControlID_Label2 Top= Left=0 Caption=Enter Name Here";
ComplexReplace(matchPattern, source);
}
only the
Top
attributes of the
ControlID_*
lines are changed from their original values
to
.
The result of this replace action will change
the Top attribute value of a
ControlID_*
line to zero if
it is less than zero or greater than 5000. Any other tag that
contains a Top attribute will
remain
unchanged. The following three
lines of the
source
string will be changed from:
ControlID_TextBox1 Top=-100 Left=0 Text=BLANK
ControlID_Label1 Top=9999990 Left=0 Caption=Enter Name Here
ControlID_Label2 Top= Left=0 Caption=Enter Name Here";
to:
ControlID_TextBox1 Top=0 Left=0 Text=BLANK
ControlID_Label1 Top=0 Left=0 Caption=Enter Name Here
ControlID_Label2 Top=0 Left=0 Caption=Enter Name Here";
Discussion
The
MatchEvaluator
delegate, which is
automatically invoked when it is supplied as a parameter to the
Regex
class's
Replace
method, allows for custom
replacement of each string that conforms to the regular expression
pattern.
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
delegate passed
in as a parameter:
string newString = RE.Replace(source, replaceCallback);
See Also
See the ".NET Framework Regular Expressions"
topic in the MSDN documentation.