Compiling / Matching POSIX Regular Expressions in C

I am trying to match the following elements in a string pcode:

  • ufollowed by a 1 or 2 digit number
  • phaseu
  • phasep
  • x (surrounded by characters other than words)
  • y (surrounded by characters other than words)
  • z (surrounded by characters other than words)

I tried to implement regex using the POSIX regex functions (shown below), but has two problems:

  • The compiled template does not seem to have subpatterns (i.e. compiled.n_sub == 0).
  • The pattern does not find matches in the string "u0", which is really necessary!

I'm sure the regex string itself works in that it works in python and TextMate - my problem is compiling, etc. in C. Any help in getting this job would be greatly appreciated.

Thanks in advance for your answers.

if(idata=tb_find(deftb,pdata)){
    MESSAGE("Global variable!\n");
    char pattern[80] = "((u[0-9]{1,2})|(phaseu)|(phasep)|[\\W]+([xyz])[\\W]+)";
    MESSAGE("Pattern = \"%s\"\n",pattern);
    regex_t compiled;
    if(regcomp(&compiled, pattern, 0) == 0){
        MESSAGE("Compiled regular expression \"%s\".\n", pattern);
    }

    int nsub = compiled.re_nsub;
    MESSAGE("nsub = %d.\n",nsub);
    regmatch_t matchptr[nsub];
    int err;
    if(err = regexec (&compiled, pcode, nsub, matchptr, 0)){
        if(err == REG_NOMATCH){
            MESSAGE("Regular expression did not match.\n");
        }else if(err == REG_ESPACE){
            MESSAGE("Ran out of memory.\n");
        }
    }
    regfree(&compiled);
}
+3
1

, - Regex POSIX. POSIX , "" ( "" ) "" . , REG_EXTENDED regcomp:

...
if(regcomp(&compiled, pattern, REG_EXTENDED) == 0){
...

regcomp "" . , :

  • |
  • , \( \)

, POSIX 1:1 Python ( TextMate). , , POSIX , , :

 [\\W]

POSIX :

 [^[:space:]]

POSIX : C:

 char *pattern = "((u[0-9]{1,2})|(phaseu)|(phasep)|[^[:space:]]+([xyz])[^[:space:]]+)";
+14

Source: https://habr.com/ru/post/1711071/


All Articles