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 = ;
pcre *re = NULL;
int erroffset;
const char *pcError = NULL;
size_t ovecCount = ; if (NULL == pcSrc || NULL == pattern || NULL == userData ||NULL == callback)
{
return -;
} /*
子串的个数就是正则表达式中()的个数
*/
//计算子串的个数
ovecCount = matchOperator(pattern, '(', ')') + ; //编译正则表达式的pcre内部表示结构
re = pcre_compile(pattern, , &pcError, &erroffset, NULL);
if (NULL == re)
{
return -;
} //匹配字符串
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 = ;
int *offsetVector = NULL;
int offset = ;
int ovecSize = ;
int match = ; //分配内存空间
//ovecSize should be a multiple of 3
ovecSize = ovecCount * ;
offsetVector = (int *)malloc(ovecSize * sizeof(int));
if (NULL == offsetVector)
{
return -;
}
memset(offsetVector, , ovecSize * sizeof(int)); while ()
{
//匹配正则表达式
////offset为偏移量,为了循环匹配
rc = pcre_exec(re, NULL, pcSrc, strlen(pcSrc), offset, , offsetVector, ovecSize);
if (rc <= )
{
break;
}
//用户自处理
callback(pcSrc, offsetVector, rc, userData);
//偏移量赋值
offset = offsetVector[];
match++;
} return match > ? : -; }
#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[] = { };
int i = ; for (i = ; i < size; i++)
{
//regVector[2 * i]表示开始位置
//regVector[2 * i + 1] 表示结束位置
strncpy(gcData, pcData + regVector[ * i], regVector[ * i + ] - regVector[ * 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 = ; //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 ;
} #endif
Sword pcre库使用的更多相关文章
- Linux下编译安装PCRE库
备注:如果没有root权限,使用 --prefix 指定安装路径 ./configure --prefix=/home/work/tools/pcre-8.xx =================== ...
- centos7下安装pcre库(pcretest)
在linux下需要对正则表达式的验证,使用的验证工具是pcretest,这个工具集成在pcre库中,下面是安装教程. 安装环境是centos7. 1)首先去官网下载压缩包文件. 其他的source网站 ...
- Linux 安装 nginx 安装PCRE库
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库.这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是 ...
- pcre库
pcre : perl compatible regular expressions , perl 兼容正则表达式 www.pcre.org 按装pcre是为了使Nginx支持具备URI重写功能的 ...
- Sword pcre库函数学习三
14.pcre_get_substring_list 原型: #include <pcre.h> int pcre_get_substring_list(const char *subje ...
- Sword pcre库函数学习二
9.pcre_free_substring_list 原型: #include <pcre.h> void pcre_free_substring_list(const char **st ...
- Sword pcre库函数学习一
0.pcre_exec 原型: #include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, con ...
- Sword 第三方库介绍二
/* uuid生成 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <ass ...
- Sword 第三方库介绍一
/* 获取字符编码 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <str ...
随机推荐
- 在vultr中安装coreos
1.coreos必须使用key文件. 2.生成ssh key -C "your_email@mail.com" 3.拷贝ssh公钥文件内容.默认为id_rsa.pub 4.编辑vu ...
- UIViewContrller之间切换的几种方式
转自:http://blog.csdn.net/likendsl/article/details/7542296 1.UIViewContrller之间的切换有三种方式: 一.UIView ...
- 有关https安全的相关内容介绍
Https 介绍什么是Https HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道.简单讲是H ...
- haproxy rpm制做
[root@c01 tmp]# fpm -s dir -t rpm -v 1.7.7 -n haproxy --before-install /tmp/haproxy_before.sh --afte ...
- Why-are-GPUs-well-suited-to-deep-learning
https://www.quora.com/Why-are-GPUs-well-suited-to-deep-learning http://timdettmers.com/2015/03/09/de ...
- 不忘初心,回归本质 .net core
static void Main(string[] args) { ; object j = i;//装箱 就是把值类型转换成引用类型 int k = (int)j;//拆箱 就是把引用类型转换成值类 ...
- 《深入应用C++11:代码优化与工程级应用》开始发售
我的新书<深入应用C++11:代码优化与工程级应用>已经开始在华章微店发售了,下面是链接. 京东发售链接 china-pub发售链接 亚马逊发售链接 天猫商城发售链接 适用读者:C++11 ...
- 查准与召回(Precision & Recall)
Precision & Recall 先看下面这张图来理解了,后面再具体分析.下面用P代表Precision,R代表Recall 通俗的讲,Precision 就是检索出来的条目中(比如网页) ...
- Java类的成员初始化顺序
Java类的成员初始化顺序 2017-06-01 代码: public class InitializeSequence { public static void main(String[] args ...
- 在Mac上安装与使用mitmproxy
[本文出自天外归云的博客园] 介绍 Mitmproxy是一款支持HTTP(S)的中间人代理工具.不同于Fiddler2,burpsuite等类似功能工具,mitmproxy可在终端下运行,并且支持编写 ...