正则表达式返回错误结果

时间:2018-12-03 11:06:33

标签: c++ regex

我正在尝试使用正则表达式构建程序

“ regexec”返回0(这意味着该行与regex兼容),我不知道为什么。 我正在使用https://regex101.com/检查我的正则表达式。

这是我的代码:

int main ()
{  
 regex_t regex_32,regex_8;
 int reti;     
 string s32("00001206 ffffff00 00200800 00001044");
 string s8("00 00 00 00 00 00 00 00 11 11 11 11 38 00 20 00");  
 reti = regcomp(&regex_32, "([0-9a-fA-F]{8}( |$))+$", REG_EXTENDED);
 if (reti) {
    fprintf(stderr, "Could not compile regex\n");
    return (1);
 }
 reti = regcomp(&regex_8, "([0-9a-fA-F]{2}( |$))+$", REG_EXTENDED);
 if (reti) {

    fprintf(stderr, "Could not compile regex\n");
    return (1);
 }
 int ret = regexec(&regex_8, s8.c_str(), 0, NULL, 0);
 if (!ret) {
        cout<<"s8 ok"<<endl;
    }
 ret = regexec(&regex_8, s32.c_str(), 0, NULL, 0);
 if (!ret) {
        cout<<"s32 with regex8->not ok"<<endl;
    }
ret = regexec(&regex_32, s32.c_str(), 0, NULL, 0);
if (!ret) {
        cout<<"s32 ok"<<endl;
    }
    ret = regexec(&regex_32, s8.c_str(), 0, NULL, 0);
if (!ret) {
        cout<<"s8 with regex32->not ok"<<endl;
    }
   return 0;
 } 

实际输出为:

s8 ok
s32 with regex8->not ok
s32 ok

预期输出为:

s8 ok    
s32 ok

出什么问题了?

0 个答案:

没有答案
相关问题