We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.

How to: Use Regular Expressions to Extract Data Fields (C++/CLI)

The following code example demonstrates the use of regular expressions to extract data from a formatted string. The following code example uses the Regex class to specify a pattern that corresponds to an e-mail address. This patter includes field identifiers that can be used to retrieve the user and host name portions of each e-mail address. The Match class is used to perform the actual pattern matching. If the given e-mail address is valid, the user name and host names are extracted and displayed.

// Regex_extract.cpp
// compile with: /clr
#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;

int main()
    array<String^>^ address=

    Regex^ emailregex = gcnew Regex("(?<user>[^@]+)@(?<host>.+)");

    for (int i=0; i<address->Length; i++)
        Match^ m = emailregex->Match( address[i] );
        Console::Write("\n{0,25}", address[i]);

        if ( m->Success ) 
            Console::Write("   User='{0}'", 
            Console::Write("   Host='{0}'", 
            Console::Write("   (invalid email address)");

    return 0;