
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
572
|
Chapter 10: Regular Expressions
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
0.
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