判断是否是合法的QQ号:
#include <iostream>
#include <regex>
#include <string>
int main()
{
//[0-9]{5,11} 5 <= times(0~9) << 11
//std::regex str_reg("[1-9]([0-9]{5,11})");
//\\d{5,11} 5 <= times(0~9) << 11
std::regex str_reg("[1-9](\\d{5,11})");
std::string str_in("1032057405"); //1032057d05 0032057405
std::smatch matchResult;
//正则匹配
if (std::regex_match(str_in, matchResult, str_reg))
{
std::cout << "Match:" << std::endl;
//打印子表达式结果
for (size_t i = 0; i < matchResult.size(); ++i)
{
std::cout << matchResult[i] << std::endl;
}
}
else
{
std::cout << "Not Match!" << std::endl;
}
return 0;
}
判断是否是合法的手机号:
#include <iostream>
#include <regex>
#include <string>
int main()
{
//[0-9]{10} times(0~9) = 10
std::regex str_reg("1([0-9]{10})");
//\\d{10} times(0~9) = 10
//std::regex str_reg("1(\\d{10})");
std::string str_in("18321768967"); //1s321768967 28321768967 1832176896
std::smatch matchResult;
//正则匹配
if (std::regex_match(str_in, matchResult, str_reg))
{
std::cout << "Match:" << std::endl;
//打印子表达式结果
for (size_t i = 0; i < matchResult.size(); ++i)
{
std::cout << matchResult[i] << std::endl;
}
}
else
{
std::cout << "Not Match!" << std::endl;
}
return 0;
}