"regex.h"的基本使用

下面是简单写的案例,详细的参数参见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的时候被这个坑了很长时间

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值