使用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);
}