Regular Expressions Sample

Sample Overview

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.

Common Useful Regular Expressions.

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}

Sample Source and Build Output Locations

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.

Building the Sample

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.

Running the Sample

These steps require that the Shared Source CLI (SSCLI) be already built and functional.
  1. Run env.bat or source the env.csh or env.sh script files depending on your platform.
  2. Switch to the %TARGETCOMPLUS%\samples\howto\regularexpressions directory.
  3. Type the following command:

    clix regularexpressions.exe


Copyright (c) 2002 Microsoft Corporation. All rights reserved.