C++11 Standard STL Regular expression verification e-mail address

Source: Internet
Author: User
Tags visual studio 2010

Transferred from: http://www.cnblogs.com/yejianfei/archive/2012/10/07/2713715.html

The most common verification we encounter is email address verification. Common on the site. Various Web page scripts are also commonly used "regular Expressions" (Regular expression) to verify the e-mail address we enter and determine whether it is legal. Some can also break down the user name and domain name. Now implementation of the e-mail address validator in the C + + language, with the new C + + 11 standard added STL regular expression.

The source code is as follows, and the code has been validated on Visual Studio 2010. g++ 4.6 does not support C + + 11 STL Regular Expressions,g++ 4.6 can be compiled through, but run-time errors, throw regex_error exceptions. Therefore, if you want to use regular expressions on g++ 4.6, use the GNU Regular Expression library or the boost regular expression library.

1 /*2 * Regex.cpp-Verify e-mail address with regular expression3  *4 * C++11 Standard STL Regular Expression5  *6  *7 * Copyright Ye Jianfei8  *9 * Compile command:Ten * CL Regex.cpp/ehsc/link/out:regex.exe One  * A  */ -  -#include <iostream> the#include <cstdlib> -#include <string> -#include <regex>//Regular expression Regular Expressions -  + using namespacestd; -  + intMain () A { at     stringemail_address; -     stringuser_name, domain_name; -  -Regex pattern ("([0-9a-za-z\\-_\\.] +) @ ([0-9a-z]+\\. [A-z] {2,3} (\\. [A-z] {2})?)"); -     //regular expressions, matching rules: -     //group 1th (i.e. user name), matching rules: 0 to 9, A to Z, A to Z, underscore, dot, hyphen in     //any character, repeat over or above -     //Middle, an "@" symbol to     //The second group (that is, the domain name), matching rules: 0 to 9 or a to Z of any character repeats or more, +     //then a point, and then any character in A to Z repeats 2 to 3 times (such as COM or CN, etc.), -     //a group within the second group, a point, followed by any character in A to Z 2 times (e.g. CN or FR, etc.) the     //a whole group of internal repeats 0 or one time *  $ Panax Notoginseng     //Enter end of file (Windows uses Ctrl+z,unix with Ctrl+d) to close the loop -      while(Cin >>email_address) the     { +         if(Regex_match (email_address, pattern)) A         { thecout <<"The email address you entered is legal"<<Endl; +  -             //intercept the first group $user_name = Regex_replace (email_address, pattern,string(" $") ); $  -             //Intercept the second group -domain_name = Regex_replace (email_address, pattern,string(" $") ); the  -cout <<"User name:"<< user_name <<Endl;Wuyicout <<"Domain Name:"<< domain_name <<Endl; thecout <<Endl; -         } Wu         Else -         { Aboutcout <<"The email address you entered is not valid"<< Endl <<Endl; $         } -     } -     returnexit_success; -}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.