gpt4 book ai didi

c++ - Regexec() 没有给出预期的结果

转载 作者:搜寻专家 更新时间:2023-10-31 02:13:08 24 4
gpt4 key购买 nike

我需要检查月份是否为 01-12 的形式。我制作了一个正则表达式并输入以检查它是否与正则表达式匹配。代码-

#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;
}

输入一 = 09;星期一=09;输出为“否”

但是 09 匹配给定的正则表达式

最佳答案

正则表达式似乎是正确的。尝试使用 C++11 中的正则表达式

cout << "Hello World" << endl;
string mon;
cin >> mon;
string monthRegex = "(0[1-9]|1[0-2])";
std::regex rex ( monthRegex );

if ( std::regex_match ( mon, rex ) )
{
std::cout << "Matched\n";
}
else
{
std::cout << "Not matched\n";
}

关于c++ - Regexec() 没有给出预期的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41804570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com