• Sword pcre库使用


    #include <stdlib.h>
    #include <string.h>
    
    #include "regularhelper.h"
    #include "pcre/pcre.h"
    #include "stringhelper.h"
    
    int regularLoop(pcre *re, char *pcSrc, size_t ovecCount, void *userData, FuncHandle callback);
    
    /********************************************************
       Func Name: regularInfer
    Date Created: 2018-9-29
     Description: pcre识别
           Input: 
          Output: error code
         Caution: 
    *********************************************************/
    int regularInfer(char *pcSrc, const char *pattern, void *userData, FuncHandle callback)
    {
        int result = 0;
        pcre *re = NULL;
        int erroffset;
        const char *pcError = NULL;
        size_t ovecCount = 0;
    
        if (NULL == pcSrc || NULL == pattern || NULL == userData ||NULL == callback)
        {
            return -1;
        }
    
        /*
        子串的个数就是正则表达式中()的个数
        */
        //计算子串的个数
        ovecCount = matchOperator(pattern, '(', ')') + 1;
        
        //编译正则表达式的pcre内部表示结构
        re = pcre_compile(pattern, 0, &pcError, &erroffset, NULL);
        if (NULL == re)
        {
            return -1;
        }
    
        //匹配字符串
        result = regularLoop(re, pcSrc, ovecCount, userData, callback);
    
        //清理资源
        pcre_free(re);
    
        return result;
    }
    
    /********************************************************
       Func Name: regularLoop
    Date Created: 2018-9-29
     Description: 循环识别字符串
           Input: 
          Output: error code
         Caution: 
    *********************************************************/
    int regularLoop(pcre *re, char *pcSrc, size_t ovecCount, void *userData, FuncHandle callback)
    {
        int rc  = 0;
        int *offsetVector = NULL;
        int offset = 0;
        int ovecSize = 0;
        int match = 0;
        
        //分配内存空间
        //ovecSize should be a multiple of 3
        ovecSize = ovecCount * 3;
        offsetVector = (int *)malloc(ovecSize * sizeof(int));
        if (NULL == offsetVector)
        {
            return -1;
        }
        memset(offsetVector, 0, ovecSize * sizeof(int));
    
        while (1)
        {
            //匹配正则表达式
            ////offset为偏移量,为了循环匹配
            rc = pcre_exec(re, NULL, pcSrc, strlen(pcSrc), offset, 0, offsetVector, ovecSize);
            if (rc <= 0)
            {
                break;
            }
            //用户自处理
            callback(pcSrc, offsetVector, rc, userData);
            //偏移量赋值
            offset = offsetVector[1];
            match++;
        }
    
        return match > 0 ? 0 : -1;
    
    }
    #ifdef TEST
    
    #include "regularhelper.h"
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void deal(char *pcData, int *regVector, size_t size, void *userArg)
    {
        char gcData[1024] = { 0 };
        int i = 0;
    
        for (i = 1; i < size; i++)
        {
            //regVector[2 * i]表示开始位置
            //regVector[2 * i + 1] 表示结束位置
            strncpy(gcData, pcData + regVector[2 * i], regVector[2 * i + 1] - regVector[2 * i]);
            printf("key is %s
    ", gcData);
        }
        printf("
    ", gcData);
    }
    
    void test()
    {
        char *p = NULL;
        //char str[] = "http://1.203.80.138:8001/tts?user_id=speech&domain=1&language=zh&audiotype=6&rate=1&speed=5&text=asr error goodbye";
        char str[] = "tabceftsfasdft12345t";
        int result = 0;
        
        //memset(&data, 0, sizeof(STParamList));
        int data;
    
        //提取所有的参数
        result = regularInfer(str, "t(.)(.)(.)(.)(.)t", &data, deal);
        if (result)
        {
            printf("regularInfer() error .
    ");
        }
    }
    
    int main(int argc,char * argv[])
    {
        test();
        return 0;
    }
    
    #endif
  • 相关阅读:
    iview-cli 采坑记录
    js关于可视区高度的一些运算
    时间戳格式化
    快应用入坑指南
    webpack开发环境配置和生产环境配置
    webpack深入场景——开发环境和生产环境
    react-redux状态管理思想
    禁用浏览器双击时,选中文字的特性
    宝塔7.4.7专业版安装Nginx防火墙后,网站报错:ERR_CONNECTION_REFUSED的解决。
    帝国CMS修改模板或数据报错“您的请求带有不合法参数,已被网站管理员设置拦截!”
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9733231.html
Copyright © 2020-2023  润新知