HOW TO: Using regular expressions and Visual C # .NET matching mode (from MSDN)

xiaoxiao2021-03-06  75

The release number of this article has been CHS308252

This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.

For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.

For Microsoft C # .NET versions of this article, see

301264.

This task content

summary

Requirements to use regular expression matching mode reference

SUMMARY This article demonstrates how to create and use regular expressions to determine if the string matches some modes. The character string can be easily analyzed using a regular expression and a string matches a particular mode. use

The object of the RegularExpressions Space, you can compare a string with a given mode, replace a string mode with another string, or only the specific part of the format string. In this example, we will construct a mode to verify an email address.

Back to top

The following list summarizes the recommended hardware, software, network architecture, and service pack required:

Microsoft Visual C # .net This article assumes you:

Understand Visual C # .net basically understand the regular expression

Back to top

Use regular expression matching mode

Open Visual Studio .NET. Create a Visual C # console application. In the Text.RegularExpressions name space specifying the USING keyword, so if you encounter these namespace in the back code, you don't need to limit the declaration. The USING statement must be used before any other statement. Using System.Text.RegularExpressions; Defines a new regular expression that will use the pattern match to verify the email address. This regular expression constructed below has three functions:

Capture the child string before the @ symbol and put it in the "User" group. Capture the child after the @ symbol and put it in the "host" group. Make sure there is no @ symbol in the first half of the string. Regex emailRegex = new regex ("(? . )"); Define a new string containing a valid email address. This will provide a default value when the method's command line parameters are empty: string s = "johndoe@tempuri.org"; check if there is a command line parameter; if there is, take the first parameter and give it a variable "S ". IF (args.length> 0) {

s = args [0];

} Use the match method to pass the email address variable and return a new Match object. Whether you find a matching item in the source string, the MATCH object will return. Match m = emailRegex.match (s); by checking the SUCCESS attribute, we can decide whether to continue to process the Match object or display an error message. If successful, a group named "User" and "Host" is displayed in the Groups collection of the Match object. IF (m.success) {

Console.Writeline ("User:" M.Groups ["User"]. Value;

Console.writeline ("Host:" M.Groups ["Host"]. Value);

} else {

Console.writeLine (S "Is Not a Valid Email Address);

Console.writeline (); If you want to keep the console window after running the application, you can add the following line of code: System.Console.writeline ("Press Enter to Continue ...");

System.console.readline (); generated project. If the default email address specified in the code is running the application in the development environment, press F5 or select the start button in the debug menu. There are two options to choose from with a command line parameter.

Open a command window and locate the "bin / debug" folder under the folder where your project is located. Then type the name of the executable, followed by the email address to be tested. Find the executable of this project, drag it to the Start ... Run window on the taskbar. Add the email address to be verified and click or press OK.

Back to top

reference

Regular expression syntax http://msdn.microsoft.com/scripting/default.htm?/scripting/vbscript/doc/jsgrpregexpsyntax.htm regular expression Introduction http://msdn.microsoft.com/scripting/default.htm? /scripting/vbscript/doc/reconintroductionToreGularExpressions.htm Regulation Language Element (Microsoft .NET Framework General Reference) Regex class (Microsoft .NET Framework class library) As a regular expression (Microsoft .NET Framework Development Guide)

Back to top

The information in this article applies to:

Microsoft Visual C # .NET Beta 2

Recent Updated: 2001-10-26 (1.0) Keyword KBDsupport Kbhowto KbhowTomaster KB308252 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-109004.html

New Post(0)