標籤:空間 匹配 結構 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庫使用