neb*_*eby 2 c regex posix posix-ere
我试图在C中使用POSIX正则表达式,但它不起作用.这是我的代码:
int regi(char *c, char *e)
{
regex_t regex;
int reti = regcomp(®ex, e, 0);
reti = regexec(®ex, c, 0, NULL, 0);
if(!reti)
return 1;
return 0;
}
int main()
{
char str[5] = {'0','x','2','F'};
if(regi(str, "^(0[xX])[0-9a-fA-F]+"))
// Do stuff
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在这看:http://www.peope.net/old/regex.html
这永远不会进入if语句.
要使用+元字符,您需要告诉regcomp()您正在使用扩展的POSIX语法:
int reti = regcomp(®ex, e, REG_EXTENDED);
Run Code Online (Sandbox Code Playgroud)
但是,此代码还存在其他几个问题,包括转换const char*为char*和忽略返回值regcomp().