Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 78 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
78
Dung lượng
2,07 MB
Nội dung
Method Description IsMatch Returns a Boolean value that indicates whether the regular expression pattern is matched in the string, which is the argument to the IsMatch() method. Match Returns zero or one Match object, depending on whether the string supplied to the method as its argument contains a match. Matches Returns a MatchCollection object containing zero or more Match objects, which contain all matches (or none) in the string that is the argument to the Matches() method. Replace Replaces all occurrences of a regular expression pattern with a specified character sequence. Split Splits an input string into an array of strings. The split occurs at a position indicated by a regular expression pattern. ToString Returns a string containing the regular expression passed into the Regex object in its constructor. Unescape Unescapes any escaped characters in the input string. The CompileToAssembly() Method The Regex class’s CompileToAssembly() method takes two arguments: the RegexCompilationInfo object (which is a member of the System.Text.RegularExpressions namespace and contains the information necessary to specify how compilation is to be carried out) and the name of the assembly to be created. When the CompileToAssembly() method is used, the startup time can be expected to increase but with the benefit of faster running. The GetGroupNames() Method The GetGroupNames() method retrieves the names of any named groups associated with a Match object. The GetGroupNames() method takes no argument. The GetGroupNumbers() Method The GetGroupNumbers() method retrieves the numbers of any numbered groups associated with a Match object. There is always at least one group, which matches the entire regular expression pattern. If paired parentheses are included in the regular expression pattern, there may be additional numbered groups. The GetGroupNumbers() method takes no argument. GroupNumberFromName() and GroupNameFromNumber() Methods The GroupNumberFromName() method retrieves a group number given a group name as its argument. The group’s name is supplied as a string argument. The GroupNameFromNumber() method retrieves a group name, if one exists, for a group number supplied as the method’s argument. The group’s number is supplied to the method as an int argument. 519 C# and Regular Expressions 25_574892 ch22.qxd 1/7/05 11:15 PM Page 519 The IsMatch() Method The Regex object’s IsMatch() method takes a single string argument and tests whether the regular expression pattern is matched in that string argument. It returns a bool value. Optionally, the IsMatch() method takes a second argument, an int value, which specifies the position in the string argument at which the attempt at matching is to begin. Try It Out The IsMatch() Method 1. Open Visual Studio 2003, create a new application from a console application template, and name the new project IsMatchDemo. 2. Add the following statement after the using System; statement: using System.Text.RegularExpressions; 3. Edit the content of the Main() method as follows: Console.WriteLine(@”This will find a match for the regular expression ‘[A-Z]\d’.”); Console.WriteLine(“Enter a test string now.”); Regex myRegex = new Regex(@”[A-Z]\d”, RegexOptions.IgnoreCase); string inputString; inputString = Console.ReadLine(); Match myMatch = myRegex.Match(inputString); string outputString = “The following option(s) are set: “; Console.WriteLine(outputString + myRegex.Options.ToString()); Console.WriteLine(“You entered the string: ‘“ + inputString + “‘.”); if (myRegex.IsMatch(inputString)) Console.WriteLine(“The match ‘“ + myMatch.ToString() + “‘ was found in the string you entered.”); else Console.WriteLine(“No match was found.”); Console.ReadLine(); 4. Save the code, and press F5 to run it. 5. Enter the test string J88 at the command-line prompt; press Return; and inspect the displayed information, as shown in Figure 22-3. Figure 22-3 The IsMatch() method can also be statically overloaded so that you can use it without having to instan- tiate a Regex object. 520 Chapter 22 25_574892 ch22.qxd 1/7/05 11:15 PM Page 520 How It Works As in the first example in this chapter, a Regex object is instantiated and assigned to the object variable myRegex with the regular expression pattern [A-Z]\d. Because there is a metacharacter that includes a backslash, the @ character precedes the string argument in the Regex() constructor, so you need not double the backslash: Regex myRegex = new Regex(@”[A-Z]\d”, RegexOptions.IgnoreCase); After the user has entered a string, the IsMatch() method is used to determine whether the entered string does or does not contain a match: if (myRegex.IsMatch(inputString)) When the test string contains a match, the bool value True is returned. In this example, the content of the if statement is, therefore, processed. If no match is found, the bool value False is returned, and the else statement is processed: else Console.WriteLine(“No match was found.”); Because the regular expression pattern is [A-Z]\d, the match from the test string J88 is J8. The Match() Method The Match() method has the following overloaded methods: public Match Match(string, inputString); and: public Match Match(string inputString, int startAt); and: public Match Match(string inputString, int startAt, int length); The inputString argument is tested to determine whether a match is present for the regular expression pattern contained in the Regex object. As you saw in the first example in this chapter, the Match() method returns a Match object: Match myMatch = myRegex.Match(inputString); The Match() method is used when you want to find out whether or not there is a match in a test string. The Regex object’s Match() method can be used together with the Match object’s NextMatch() method to iterate through all matches in a test string. This usage is further discussed in conjunction with the Match object a little later in this chapter. The Match() method can also be used as a static method, as discussed later in this chapter. 521 C# and Regular Expressions 25_574892 ch22.qxd 1/7/05 11:15 PM Page 521 The Matches() Method When you want to find all the matches in a test string, the Matches() method is the one to use. Try It Out The Matches() Method 1. Open Visual Studio 2003, create a new project from the Windows Application template, and name the new project MatchesDemo. Figure 22-4 shows the screen’s appearance. Depending on how you have set options for Visual Studio 2003, the appearance that you see may differ slightly. Figure 22-4 2. Drag a Label control from the Toolbox to close to the top of the form, and change its Text prop- erty to This form tests against the pattern ‘[A-Z]\d’. 3. Drag a Label control from the Toolbox onto the form, and change its Text property to Enter a test string: 4. Drag a TextBox control from the Toolbox to the form’s design surface, and change its Text prop- erty to be blank. 522 Chapter 22 25_574892 ch22.qxd 1/7/05 11:15 PM Page 522 5. Drag a Button control to the form, and change its Text property to Click to Find Matches. 6. Drag a TextBox control to the form, and change its Multline property to True and its Text prop- erty to be blank. Figure 22-5 shows the desired appearance after this step. You will likely have to tweak the position and size of the controls to achieve an appearance similar to the one shown. Figure 22-5 At this stage, the form looks reasonably tidy but has no functionality associated with it. Now the code must be created to specify that you are using the System.Text.RegularExpressions namespace. 7. In the Solution Explorer, right-click Form1.cs and select View Code to open the code editor. Scroll up, if necessary, and you will see several using statements: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; 8. After the final automatically created using statement, add the following new line of code: using System.Text.RegularExpressions; 523 C# and Regular Expressions 25_574892 ch22.qxd 1/7/05 11:15 PM Page 523 9. Return to the design surface. Double-click the Click to Find Matches button. The code editor will open with the following code automatically created for you: private void button1_Click(object sender, System.EventArgs e) { } The button1_Click event handler responds to a click on the button. You now need to add code to create some functionality when that button is clicked. 10. In the code editor, add the following code between the opening brace and closing brace of the button1_Click event handler: Regex myRegex = new Regex(@”[A-Z]\d”); string inputString; inputString = this.textBox1.ToString(); MatchCollection myMatchCollection = myRegex.Matches(inputString); this.textBox2.Text = “The matches are:” + Environment.NewLine; foreach(Match myMatch in myMatchCollection) { this.textBox2.Text += myMatch.ToString() + Environment.NewLine; } 11. Save the code, and press F5 to run it. If the code does not run, take a look at the error messages that appear in the build errors task list. Note the line number that is mentioned in the first error, and attempt to locate and correct that error. Then press F5 to see whether any subsequent errors have also been remedied by correcting the first error. If you entered the code correctly, you should see a screen with an appearance similar to that shown in Figure 22-6. The exact appearance will depend on how you positioned the form con- trols and sized the form. 12. Enter the test string K99 L00 M11 in the upper text box. 13. Click the Click to Find Matches button, and inspect the results displayed in the lower text box, as shown in Figure 22-7. Notice that three matches are displayed in the lower text box. How It Works To use classes from the System.Text.RegularExpressions namespace, you must add an appropriate using directive: using System.Text.RegularExpressions; The work of the simple application is carried out by the code inside the button1_Click function. First, an object variable, myRegex, is declared as inheriting from the Regex class and is assigned the regular expression pattern [A-Z]\d, using the @ syntax to avoid having to double backslash characters inside the paired double quotes. Regex myRegex = new Regex(@”[A-Z]\d”); 524 Chapter 22 25_574892 ch22.qxd 1/7/05 11:15 PM Page 524 Figure 22-6 Figure 22-7 525 C# and Regular Expressions 25_574892 ch22.qxd 1/7/05 11:15 PM Page 525 Next, a string variable, inputString, is declared: string inputString; Next, the value entered into textBox1 (the single line, upper text box) is assigned to the inputString variable: inputString = this.textBox1.ToString(); Next, an object variable, myMatchCollection, is declared to inherit from the MatchCollection class. A MatchCollection object can contain zero or more Match objects. You populate the myMatchCollection variable using the myRegex variable’s Matches() method, supplying the inputString variable as the argument to the Matches() method: MatchCollection myMatchCollection = myRegex.Matches(inputString); Next, assign some literal text to the Text property of textBox2. The Environment.Newline is used to cause the display to move to a new line: this.textBox2.Text = “The matches are:” + Environment.NewLine; Then you use a foreach statement to add further text to textBox2 for each Match object contained in the myMatchCollection variable: foreach(Match myMatch in myMatchCollection) { this.textBox2.Text += myMatch.ToString() + Environment.NewLine; } The Replace() Method The Regex class’s Replace() method allows character sequences that match a pattern to be replaced by a specified pattern or sequence of characters. Try It Out the Replace() Method 1. Create a new console application in Visual Studio 2003, and name the new project SimpleReplace. 2. In the code editor, make edits so that the code matches the following, Class1.cs: using System; using System.Text.RegularExpressions; namespace SimpleReplace { class Class1 { [STAThread] static void Main(string[] args) { 526 Chapter 22 25_574892 ch22.qxd 1/7/05 11:15 PM Page 526 Console.WriteLine(@”This will find a match for the regular expression ‘wrox’”); Console.WriteLine(@”and replace it with ‘Wrox’.”); Console.WriteLine(“Enter a test string now.”); Regex myRegex = new Regex(@”wrox”, RegexOptions.IgnoreCase); string inputString; inputString = Console.ReadLine(); string newString = myRegex.Replace(inputString, “Wrox”); Console.WriteLine(“You entered the string ‘“ + inputString + “‘.”); Console.WriteLine(“After replacement the new string is ‘“ + newString + “‘.”); Console.ReadLine(); } } } Be sure to include the using System.Text.RegularExpressions; directive. Save the code, and press F5 to run it. 3. In the command window, enter the sample text This book is published by wrox.; and then press the Return key and inspect the displayed results, as shown in Figure 22-8. Notice that the character sequence wrox (initial lowercase w) is replaced by Wrox (initial uppercase W). Figure 22-8 4. Press the Return key to close the command window. 5. In Visual Studio, press F5 to run the code again. 6. In the command window, enter the test string This book is published by WROX.; press the Return key; and inspect the results. Because matching is case insensitive, as specified by the IgnoreCase option, the character sequence WROX is matched and is also replaced by the charac- ter sequence Wrox. How It Works The code, as usual, includes a using System.Text.RegularExpressions; directive. First, a message is displayed that informs the user of the purpose of the application: Console.WriteLine(@”This will find a match for the regular expression ‘wrox’”); Console.WriteLine(@”and replace it with ‘Wrox’.”); The simple literal pattern wrox is assigned to the myRegex object variable. Because the IgnoreCase option is specified, wrox, Wrox, WROX, and so on will be matched: Regex myRegex = new Regex(@”wrox”, RegexOptions.IgnoreCase); 527 C# and Regular Expressions 25_574892 ch22.qxd 1/7/05 11:15 PM Page 527 [...]... which regular expression functionality is supported in PHP 5.0 552 PHP and Regular Expressions Figure 23-4 How PHP Structures Suppor t for Regular Expressions Regular expression support in PHP is provided by two different sets of functions One, ereg() and related functions, has been available in PHP for some time (you can use it with PHP3 Web servers if you wish) The other, Perl Compatible Regular Expressions. .. affecting the functionality 8 Double-click the button, and the code editor should open with the following code displayed: private void button1_Click(object sender, System.EventArgs e) { } 5 28 C# and Regular Expressions Figure 22-9 9 Scroll up to the top of the code, and below the automatically created using directives, insert the following code: using System.Text.RegularExpressions; 10 Scroll down... learn the following: ❑ How to get started with PHP 5.0 ❑ How PHP structures support for regular expressions ❑ How to use the ereg() family of functions ❑ What metacharacters are supported in PHP in Perl Compatible Regular Expressions (PCRE) ❑ How to match commonly needed user entries This chapter describes the regular expression functionality in PHP version 5.0 Getting Star ted with PHP 5.0 To run... three groups displayed for each match Finally, a further spacing line is output each time round the outer foreach loop: Console.WriteLine(); } 5 38 C# and Regular Expressions The RegexOptions Class The RegexOptions class, a member of the System.Text.RegularExpressions namespace, specifies which of the available options are or are not set The following table summarizes the options available using RegexOptions... set of functions is based on POSIX regular expressions The following table summarizes the functions that relate to the ereg() function Function Description ereg() Attempts to match a regular expression pattern against a string case sensitively eregi() Attempts to match a regular expression pattern against a string case insensitively ereg_replace() Attempts to match a regular expression pattern case sensitively,... explored no further here 541 Chapter 22 Metacharacters Suppor ted in Visual C# NET Visual C#.NET has a very complete and extensive regular expressions implementation, which exceeds in functionality many of the tools you saw in earlier chapters of this book Much of the regular expression support in Visual C# NET can reasonably be termed standard However, as with many Microsoft technologies, the standard... name the project NamedGroupsDemo 2 In the code editor, add the following line after any default using statements: using System.Text.RegularExpressions; 3 Enter the following code between the curly braces of the Main() method: Console.WriteLine(@”This will find a match for the regular expression ‘^(?\w+)\s+(?\w+)$’.”); Console.WriteLine(“Enter a test string consisting of a first name... 22-15 544 C# and Regular Expressions How It Works The content of the Main() method is explained here First, the pattern to be matched against is displayed, and the user is invited to enter a first name and last name The pattern to be matched contains two named groups, represented respectively by (?\w+) and (?\w+): Console.WriteLine(@”This will find a match for the regular expression... inputString = Console.ReadLine(); The Regex class’s Replace() method is used statically and is applied to the inputString variable, and the result is assigned to the outputString variable 546 C# and Regular Expressions The regular expression to be matched is in the second argument of the Replace() method, (\w+)\s+(\1) That pattern matches a sequence of word characters equivalent to the character class [A-Za-z0-9_]... to True Change its Text property so that it is blank 8 Size and align the form controls so that they resemble those shown in Figure 22-11 Drag a button onto the form design surface, and change its Text property to Click to find all matches 533 Chapter 22 Figure 22-11 9 Add the following code below the other using directives: using System.Text.RegularExpressions; 10 Double-click the button to create the . System.Data; 8. After the final automatically created using statement, add the following new line of code: using System.Text.RegularExpressions; 523 C# and Regular Expressions 25_57 489 2 ch22.qxd. processed: else Console.WriteLine(“No match was found.”); Because the regular expression pattern is [A-Z]d, the match from the test string J 88 is J8. The Match() Method The Match() method has the following. argument. 519 C# and Regular Expressions 25_57 489 2 ch22.qxd 1/7/05 11:15 PM Page 519 The IsMatch() Method The Regex object’s IsMatch() method takes a single string argument and tests whether the regular expression