我必须使用长度为15的字符串i / p。前两个字母应该是字母,接下来是13个数字。例如:AB1234567891234。如何检查前两个是否只是字母而其他只是数字?
答案 0 :(得分:6)
#include <regex>
const std::regex e("^[a-zA-Z][a-zA-Z][0-9]{13}$");
std::string str = "ab1234567890123";
if (std::regex_match (s,e))
std::cout << "string object matched\n";
答案 1 :(得分:1)
答案 2 :(得分:1)
#include <cctype>
bool is_correct(std::string const& s) {
if (s.size() != 15) return false;
if (!std::isalpha(string[0]) || !std::isalpha(string[1]))
return false;
for (std::size_t i = 2; i < 13; ++i) {
if (!std::isdigit(string[i])) return false;
}
return true;
}
答案 3 :(得分:0)
#include<iostream>
#include<string>
int main(int argc, char *argv[])
{
std::string n_s = "AB1234567896785";
bool res = true;
std::cout<<"Size of String "<<n_s.size()<<n_s.length()<<std::endl;
int i = 0, th = 2;
while(i < n_s.length())
{
if(i < th)
{
if(!isalpha(n_s[i]))
{
res = false;
break;
}
}
else
{
if(!isdigit(n_s[i]))
{
res = false;
break;
}
}
i++;
}
if(res)
{
std::cout<<"Valid String "<<std::endl;
}
else
{
std::cout<<"InValid Strinf "<<std::endl;
}
return 0;
}