我需要檢查月份是否是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
對不起我還是新來的 我現在編輯了代碼 –