0

I have witten a simple c program as follows:

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

I have read the POSIX regular expressions standard on Wikipedia, I haven't found any evidence for that whether the POSIX stand suports the \d metacharacter.

Or there is a subtle mistake in my code?

1 Answer 1

1

According to this, POSIX regular expressions do not support the \d shorthand (and neither do they support \w or \s).

1
  • thx, this article eliminates my confusion. I wished to get an exact list of features of every standard, is there any stuff like this? Commented Nov 26, 2011 at 2:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.