Search code examples
cregexposix

Start (^) and end ($) anchors not working


Basically I'm using following pattern in my C program (See Regular expression matching an infinite pattern):

^[0-9]( [0-9])*$

with following code:

char *pattern = "^[0-9]( [0-9])*$";
regex_t regexCompiled;
int rc = regcomp(&regexCompiled, pattern, REG_EXTENDED);
if (rc != 0) {
    char msgbuf[100];
    regerror(rc, &regexCompiled, msgbuf, sizeof (msgbuf));
    fprintf(stderr, "Regex match failed: %s\n", msgbuf);
    exit(EXIT_FAILURE);
}

if (regexec(&regexCompiled, "0 1", 0, NULL, REG_EXTENDED) == 0) {
    printf("Valid\n");
} else {
    printf("Invalid\n");
}

Where I exec against the string "0 1", which is valid for the pattern and it's not working. The '^' and '$' are not functioning. Why is that? and how can I make it work?


Solution

  • You are passing REG_EXTENDED to regexec(), that's not a valid flag for that call.

    The manual page says:

    eflags may be the bitwise-or of one or both of REG_NOTBOL and REG_NOTEOL which cause changes in matching behavior described below.

    Probably the actual value of REG_EXTENDED matches one of those.

    Changing the code to pass 0 as the final argument to regexec() makes it match.