-1

here are my codes:

#include <regex.h>
#include <string.h>

char solveRegExpress(const char *pcCommand,const char* pPattern,regmatch_t* pMatch)
{
    int uFlags              =             REG_EXTENDED | REG_ICASE;
    int uStatus             =             0;
    const size_t Nmatch     =             1;
    regex_t tRegExpress;
    regmatch_t Tmatch[20];


    regcomp(&tRegExpress ,pPattern,(int)uFlags);
    uStatus   =   regexec(&tRegExpress,pcCommand,Nmatch,Tmatch,0);
    if(0 == uStatus)
    {
        if(tRegExpress.re_nsub>1)
        {
            if(pMatch!=nullptr)
            {
                pMatch->rm_so = Tmatch->rm_so;
                pMatch->rm_eo = Tmatch->rm_eo;
            }
            regfree(&tRegExpress);
            return 3;
        }
        if(pMatch!=nullptr)
        {
            pMatch->rm_so = Tmatch->rm_so;
            pMatch->rm_eo = Tmatch->rm_eo;
        }
        regfree(&tRegExpress);
        return 0;
    }
    else
    {
        regfree(&tRegExpress);
        return 1;
    }
}

char checkForStrSign(char* pcStr,int* endPos)
{
    regmatch_t sGmatch ;
    memset(&sGmatch,0,sizeof (sGmatch));
    if( 1 == solveRegExpress(pcStr,"\".*?\"(?!')",&sGmatch))
    {
        return 1;
    }
    *endPos  =  (int)sGmatch.rm_eo;
    return  0;
}


int main(int argc, char *argv[])
{
    int pos;
    checkForStrSign("str1<<\"str2\"<<str3",&pos);

    return 0;
}

it seems that the regular expression of \".*?\"(?!') cause the problem, for i fixed the fault with regular expression of \".*?\" .Now i have no idea how to use (?!) pattern in c language.How are the segmentation fault and that pattern related. help me

Lundin
  • 195,001
  • 40
  • 254
  • 396

1 Answers1

1

You need to check the return code from regcomp. Never assume a standard library function returns success, particularly when you haven't used the function before.

Posix regular expressions do not implement non-greedy repeats nor lookahead assertions. So regcomp is probably complaining about (?. Try man 7 regex for a complete list of supported regex components. Also see the regerror function (documented in man 3 regex) for converting an error status into a (somewhat) meaningful message.

rici
  • 234,347
  • 28
  • 237
  • 341