The following code example demonstrates the use of regular expressions to extract data from a formatted string. The following code example uses the
Example
В | Copy 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; } |