#include <stdlib.h>#include<string.h>#include"Regularhelper.h"#include"pcre/pcre.h"#include"Stringhelper.h"intRegularloop (Pcre *re,Char*PCSRC, size_t Oveccount,void*UserData, Funchandle callback);/******************************************************** Func name:regularinferdate created:2018-9-29 Description:pcre Identification Input:Output:error code Caution: *************************************************** ******/intRegularinfer (Char*PCSRC,Const Char*pattern,void*UserData, Funchandle callback) { intresult =0; Pcre*re =NULL; intErroffset; Const Char*pcerror =NULL; size_t Oveccount=0; if(NULL = = PCSRC | | NULL = = Pattern | | NULL = = UserData | | NULL = =callback) { return-1; } /*The number of substrings is the number of () in the regular expression*/ //count the number of substringsOveccount = Matchoperator (Pattern,'(',')') +1; //pcre internal representation structure for compiling regular expressionsRe = pcre_compile (pattern,0, &pcerror, &Erroffset, NULL); if(NULL = =re) { return-1; } //Match Stringresult =Regularloop (Re, PCSRC, Oveccount, UserData, callback); //Clean up ResourcesPcre_free (re); returnresult;}/******************************************************** Func name:regularloopdate created:2018-9-29 Description: Loop recognition string Input:Output:error code Caution: ************************************************** *******/intRegularloop (Pcre *re,Char*PCSRC, size_t Oveccount,void*UserData, Funchandle callback) { intrc =0; int*offsetvector =NULL; intoffset =0; intOvecsize =0; intMatch =0; //Allocating memory Space//Ovecsize should be a multiple of 3Ovecsize = Oveccount *3; Offsetvector= (int*)malloc(Ovecsize *sizeof(int)); if(NULL = =offsetvector) { return-1; } memset (Offsetvector,0, Ovecsize *sizeof(int)); while(1) { //Match Regular Expression ////offset is offset, in order to loop matchrc = Pcre_exec (Re, NULL, Pcsrc, strlen (PCSRC), offset,0, Offsetvector, ovecsize); if(RC <=0) { Break; } //user self-processingcallback (PCSRC, Offsetvector, RC, userData); //Offset Value Assignmentoffset = offsetvector[1]; Match++; } returnMatch >0?0: -1;}
#ifdef test#include"Regularhelper.h"#include<stdio.h>#include<stdlib.h>#include<string.h>voidDealChar*pcdata,int*regvector, size_t size,void*Userarg) { Chargcdata[1024x768] = {0 }; inti =0; for(i =1; i < size; i++) { //regvector[2 * I] indicates the starting position//regvector[2 * i + 1] indicates the end positionstrncpy (Gcdata, PcData + regvector[2* I], regvector[2* i +1]-regvector[2*i]); printf ("Key is%s\n", Gcdata); } printf ("\ n", Gcdata);}voidTest () {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 "; CharStr[] ="tabceftsfasdft12345t"; intresult =0; //memset (&data, 0, sizeof (stparamlist)); intdata; //extract all the parametersresult = Regularinfer (str,"T (.) (.) (.) (.) (.) T", &data, Deal); if(Result) {printf ("Regularinfer () error. \ n"); }}intMainintargcChar*argv[]) {test (); return 0;}#endif
Sword Pcre Library Use