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.

Example

В 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=
    {
        "jay@southridgevideo.com",
        "barry@adatum.com",
        "treyresearch.net",
        "karen@proseware.com"
    };

    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}'", 
            m->Groups["user"]->Value);
            Console::Write("   Host='{0}'", 
            m->Groups["host"]->Value);
        }
        else 
            Console::Write("   (invalid email address)");
        }

    Console::WriteLine("");
    return 0;
}

See Also

Other Resources

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



JavaScript EditorDhtml editor     Free javascript download