POSIX扩展正则表达式是否支持\ d元字符?

时间:2011-11-25 11:02:59

标签: regex

我已经找到了一个简单的c程序如下:

#include <stdio.h>
#include <regex.h>

int main(int argc, char* argv[])
{
    regex_t re;
    char *pattern = "\\d{3}([a-zA-Z]+).(\\d{2}|N/A)\\s\\1"; // use \d, but wouldn't match
    const char *target = "123Hello N/A Hello";

    regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB);

    int ret = regexec(&re, target, (size_t) 0, NULL, 0);
    if(ret == REG_NOMATCH) {
        printf("\n%s\n does not match \n%s\n", target, pattern);
    }

    pattern = "[0-9]{3}([a-zA-Z]+).([0-9]{2}|N/A)\\s\\1"; // changes to [0-9], now OK

    regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB);

    ret = regexec(&re, target, (size_t) 0, NULL, 0);
    if(ret == REG_NOMATCH) {
        printf("\n%s\n does not match \n%s\n", target, pattern);
    }

    regfree(&re);
    return 0;
}

我已经在Wikipedia上阅读了POSIX正则表达式标准,我没有找到任何证据证明POSIX支架是否支持\ d元字符。

或者我的代码中有一个微妙的错误?

1 个答案:

答案 0 :(得分:1)

根据this,POSIX正则表达式不支持\d简写(并且它们也不支持\w\s)。