这是我的代码:
#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;
}
似乎\".*?\"(?!')
的正则表达式引起了问题,因为我用\".*?\"
的正则表达式修复了错误。现在我不知道如何在c语言中使用(?!)模式分割错误和该模式如何相关。
帮帮我
答案 0 :(得分:1)
您需要检查regcomp
的返回码。永远不要以为标准库函数会返回成功,尤其是在您以前从未使用过该函数时。
Posix正则表达式不实现非贪婪重复,也不实现超前断言。因此,regcomp
可能在抱怨(?
。尝试man 7 regex
获取受支持的正则表达式组件的完整列表。另请参见regerror
函数(在man 3 regex
中进行了介绍),用于将错误状态转换为有意义的消息。