首页 > 代码库 > C++ regex
C++ regex
使用C++regex判断数字,实数,ip,电子邮件,单词,电话号,日期等格式
#include "check.h" #include <regex> #include <string> using namespace std;
///判断全为数字 bool all_digit(const string &s) { regex r("^[0-9]*$"); return regex_match(s,r); }
///判断单词 bool all_alpha(const string &s) { regex r("^[[:alpha:]]*$"); return regex_match(s,r); }
///判断全为单词或数字 bool all_alnum(const string &s) { regex r("^[[:alnum:]]*$"); return regex_match(s,r); }
///判断整数 bool is_int(const string &s) { regex r("^[-]?(0|([1-9][0-9]*))$"); return regex_match(s,r); }
///判断实数 bool is_float(const string &s) { regex r("^-?(0|([1-9][0-9]*))\\.[0-9]+$"); return regex_match(s,r); }
///判断ip bool 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); }
///判断电话号,以0或1开头,11位数字组成 bool is_phone(const string &s) { regex r("^[01][0-9]{10}$"); return regex_match(s,r); }
///判断日期1900-2999年之间 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); }
///判断电子邮箱地址 bool is_mail(const string &s) { regex r("^[[:alnum:]]+@[[:alnum:]]+\\.[a-z]+$"); return regex_match(s,r); }
C++ regex
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。