POSIX正则表达式(在C中)匹配uuid

时间:2013-11-06 18:13:54

标签: c regex posix

我正在尝试在输入字符串中查找UUID。我在http://www.peope.net/old/regex.html处修改了示例代码并提出了这个问题:

#include <sys/types.h>
#include <regex.h>
#include <stdio.h>

int main(int argc, char *argv[]){
    regex_t regex;
    int reti;
    char msgbuf[100];

/* Compile regular expression */
    reti = regcomp(&regex, "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", 0);
    if( reti ){ fprintf(stderr, "Could not compile regex\n"); exit(1); }

/* Execute regular expression */
    reti = regexec(&regex, "'id':'0677233b-65e5-4d62-a202-2120536210d6'", 0, NULL, 0);
    if( !reti ){
            puts("Match");
    }
    else if( reti == REG_NOMATCH ){
            puts("No match");
    }
    else{
            regerror(reti, &regex, msgbuf, sizeof(msgbuf));
            fprintf(stderr, "Regex match failed: %s\n", msgbuf);
            exit(1);
    }

/* Free compiled regular expression if you want to use the regex_t again */
    regfree(&regex);

    return 0;
}

然而,它不匹配。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

{N}是一个扩展的正则表达式功能。将REG_EXTENDED标记传递给regcomp