-1

I'm trying to use regexec with the [A-Za-z0-9_-] pattern but it seems not working. For instance "AZaz09%" contains illegal character '%' but surprisingly the match is OK and the found substring size is always (int)pm.rm_eo - (int)pm.rm_so = 1 insted of 6. Does anybody know what I'm doing wrong?

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

int main()
{
    char *string = "AZaz09%";
    char *pattern = "[A-Za-z0-9_-]";
    regex_t reg;
    int match = REG_NOMATCH;

    if (regcomp(&reg, pattern, 0) == 0)
    {
        regmatch_t pm;
        match = regexec(&reg, string, 1, &pm, 0);
        printf("%d\n",(int)pm.rm_eo - (int)pm.rm_so);
    }
    regfree(&reg);
    
    if (match != REG_NOMATCH)
    {
        printf("OK");
    }
    else
    {
        printf("NOK");
    }
    return 0;
}

1 Answers1

-1

[A-Za-z0-9_-] will match ANY line that contains AT LEAST ONE occurance of those characters you listed. But it does not prevent the occurrence of other characters.

Instead, do ^[A-Za-z0-9_-]+$

The ^ means the beginning of the line and $ the end. So the above will match all lines that does not contain any other characters than the ones listed, and that also contains at least one of those characters. If you also want to allow lines of zero length, change + to *

klutt
  • 30,332
  • 17
  • 55
  • 95