Sword pcre庫使用

來源:互聯網
上載者:User

標籤:空間   匹配   結構   abc   lis   hand   提取   lang   識別   

#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: regularInferDate 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;    }    /*    子串的個數就是Regex中()的個數    */    //計運算元串的個數    ovecCount = matchOperator(pattern, ‘(‘, ‘)‘) + 1;        //編譯Regex的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: regularLoopDate 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)    {        //匹配Regex        ////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\n", gcData);    }    printf("\n", 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 .\n");    }}int main(int argc,char * argv[]){    test();    return 0;}#endif

 

Sword pcre庫使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.