C + + Regex

Source: Internet
Author: User

Use C++regex to determine the format of numbers, real numbers, IP, e-mails, words, phone numbers, dates, etc.

#include "check.h" #include <regex> #include <string>using namespace std;
The judgment is all digital bool All_digit (const string &s) {   regex R ("^[0-9]*$");   
Determine the word bool All_alpha (const string &s) {   regex R ("^[[:alpha:]]*$");    Return Regex_match (S,R);}
The judgment is all word or number bool All_alnum (const string &s) {   regex R ("^[[:alnum:]]*$");   Return Regex_match (S,R);}
Judge the integer bool Is_int (const string &s) {   regex R ("^[-]?" ( 0| ([1-9][0-9]*)) $");   Return Regex_match (S,R);}
Judge the real number bool Is_float (const string &s) {   regex R ("^-") ( 0| ([1-9][0-9]*)) \\. [0-9]+$ ");   Return Regex_match (S,R);}
Judge Ipbool is_ip (const string &s) {   regex R ("^ (((25[0-5]) | ( 2[0-4][0-9]) | (1[0-9]{2}) | (([1-9][0-9]?) | 0)) \ \.) {3} "
                        "((25[0-5)) | (2[0-4][0-9]) | (1[0-9]{2}) | (([1-9][0-9]?) | 0))) ($ ");   Return Regex_match (S,R);}
Determine the phone number, starting with 0 or 1, and 11-bit numbers make up bool Is_phone (const string &s) {   regex R ("^[01][0-9]{10}$");   Return Regex_match (S,R);}
Judgment date 1900-2,999 years between bool Is_date (const string &s) {   regex R ("^[12][0-9]{3}-(0[1-9]) | ( 1[0-2])-((0[1-9)) | ( [12] [0-9]) | (3[01])) $");   Return Regex_match (S,R);}
Determine the e-mail address bool Is_mail (const string &s) {  regex R ("^[[:alnum:]][email protected][[:alnum:]]+\\.[ a-z]+$ ");  Return Regex_match (S,R);}


C + + Regex

Related Article

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.