This sample demonstrates usage of the RegEx class and regular expressions. The user is shown a variety of regular expressions and is allowed to enter search text to which to apply the expression. To illustrate characteristics of matches, groups, and captures, these collections are used to output the expression match results to the console.
The sample has three main sections:
See source code for more detailed comments.
| Use | Expression |
|---|---|
| Social Security number | \d{3}-\d{2}-\d{4} |
| US Phone number | ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4} |
| US Postal code | \d{5}(-\d{4})? |
| Internet e-mail address | [\w-]+@([\w-]+\.)+[\w-]+ |
| Internet URL | http://([\w-]\.)+[\w-](/[\w- ./?%=]*)? |
| Simple password (digit) | ^(?=.*\d).{4,8}$ |
| Advanced password (upper, lower, digit) | ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$ |
| Common file masks | ^(.+)\\(.+)\.(.+) |
| Major credit card | \d{4}-?\d{4}-?\d{4}-?\d{4} |
The sample source is found in sscli\samples\howto\regularexpressions.
The source file is:
The build output location is %TARGETCOMPLUS%\samples\howto\regularexpressions. The output file is an executable assembly named regularexpressions.exe.
All samples are built from the buildall script.
You can also build all the
samples by switching to the root of the sample directory, sscli\samples, and typing
build -c.
You can build this specific sample by switching to the sample directory and typing
build -c.
clix regularexpressions.exe
Copyright (c) 2002 Microsoft Corporation. All rights reserved.