|
#include "stdregexp.hpp"
#include <cassert>
#include <cstdio>
#include <cwchar>
#include <cstdlib>
using namespace std;
typedef regular_expression::string_type str;
#ifdef REGEXP_UNICODE
#define _T(t) L##t
#define CHART wchar_t
#define CONVERT(a, b) do { mbstowcs(a, b, 255); a[255] = '\0'; } while(false)
#define STRF "%ls"
#else
#define _T(t) t
#define CHART char
#define CONVERT(a, b) strcpy(a, b)
#define STRF "%s"
#endif
bool parse_email(const str& to_match,
str& user_name,
str& host_and_domain,
str& domain_suffix)
{
regular_expression email_expr(_T("^([A-Za-z0-9]+)@(.+)\\.(\\a+)$"));
if(!email_expr.exec(to_match))
return false;
// the regular expression's format should ensure that all three
// expressions match, or the expression doesn't match at all.
assert(email_expr.matched(1) &&
email_expr.matched(2) &&
email_expr.matched(3));
user_name = email_expr[1];
host_and_domain = email_expr[2];
domain_suffix = email_expr[3];
return true;
}
int main(int argc, char** argv)
{
if(argc >= 2)
{
str user_name, host_and_domain, domain_suffix;
CHART buffer[255];
CONVERT(buffer, argv[1]);
if(!parse_email(buffer, user_name, host_and_domain, domain_suffix))
{
printf("Not an email address\n");
return 1;
}
printf("User name: " STRF "\nHost/domain: " STRF "\nDomain suffix: " STRF "\n",
user_name.c_str(),
host_and_domain.c_str(),
domain_suffix.c_str());
return 0;
}
printf("Usage: %s <email address>\n", argv[0]);
return 1;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
I'm a senior software developer, working at Silanis Technology (http://www.silanis.com). I've acquired quite a bit of experience (usually the hard way!) in Win32 and raw COM programming on the job. In my spare time, I like to monkey around with POSIX code.
I'm mostly interested in portable C++ libraries. I'm happiest when I develop portable C++ code--C++ being such a powerful language as long as one keeps clear of the rather nasty subtleties of the language.
I hope the articles I contribute will be of some help to someone. If even one person gains a few hours through use of that code, I'll be very happy.
When not coding, I like to listen to Anime and try to learn Japanese. It's not working too well so far, unfortunately. :{)