我需要检查月份是否是01-12的形式。我做了一个正则表达式,并采取了输入来检查它是否匹配正则表达式。 代码 -Regexec()没有给出想要的结果
#include <iostream>
#include<string>
using namespace std;
#include<regex.h>
#include<stdio.h>
int main()
{
int a;
cin>>a;
cout << "Hello World" << endl;
string mon;
cin>>mon;
string exp_month = mon;
string monthRegex = "(0[1-9]|1[0-2])";
regex_t preg;
int rc;
if (0 != (rc = regcomp(&preg, monthRegex.c_str(), REG_NOSUB))) {
cout<<("regcomp() failed, returning nonzero (%d)\n", rc);
exit(EXIT_FAILURE);
}
if (regexec(&preg,exp_month.c_str(),0,NULL,0)==0)
{
cout<<"yess";
}
else
{
cout<<"no";
}
return 0;
}
输入 A = 09; mon = 09; 输出为“无”
但09匹配给定的正则表达式
这是一个有趣的C和C++混合,为什么?另外,什么是“年”? –
这不是[最小,完整和可验证的示例](https://stackoverflow.com/help/mcve) - 您可以删除与您的问题无关的大量代码。它也不能同时是C和C++,因为它们是不同的语言。同样你的缩进可能会更好。 – Useless
对不起我还是新来的 我现在编辑了代码 –