下面是简单写的案例,详细的参数参见man regex
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
int main()
{
regex_t re;
regex_t re_de;
regex_t re_sp;
int ret0 = regcomp(&re_sp,"\\+",REG_EXTENDED);
int ret1 = regcomp(&re,"0x[0-9a-f]+",REG_EXTENDED);
int ret2 = regcomp(&re_de,"[0-9]+",REG_EXTENDED);
char e[]="330+0x5266\0";
int offset = 0;//偏移量
regmatch_t pmatch;
while(e[offset]!='\0')
{
if(regexec(&re, e+offset,1,&pmatch,0)==0 && pmatch.rm_so==0)
{
printf("hex!\n");
offset+= pmatch.rm_eo;
}
else if(regexec(&re_de, e+offset,1,&pmatch,0)==0 && pmatch.rm_so==0)
{
printf("dec!\n");
offset+= pmatch.rm_eo;
}
else if(regexec(&re_sp, e+offset,1,&pmatch,0)==0 && pmatch.rm_so==0)
{
printf("plus!\n");
offset+= pmatch.rm_eo;
}
}
return 0;
}
输出为:
这里有一个大坑,就是一定一定要把hex放在dec之前判断,不然会先识别0然后识别不出来x…这玩意,真的很坑爹,写icspa1.2的时候被这个坑了很长时间