JavaScript EditorDhtml editor     Free javascript download 

Main Page

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 email address. This patter includes field identifiers that can be used to retrieve the user and host name portions of each email address. The Match class is used to perform the actual pattern matching. If the given email address is valid, the user name and host names are extracted and displayed.


В CopyCode imageCopy Code
// 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;

See Also

Other Resources

.NET Framework Regular Expressions
.NET Programming in C++

JavaScript EditorDhtml editor     Free javascript download