#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库使用的更多相关文章

  1. Linux下编译安装PCRE库

    备注:如果没有root权限,使用 --prefix 指定安装路径 ./configure --prefix=/home/work/tools/pcre-8.xx =================== ...

  2. centos7下安装pcre库(pcretest)

    在linux下需要对正则表达式的验证,使用的验证工具是pcretest,这个工具集成在pcre库中,下面是安装教程. 安装环境是centos7. 1)首先去官网下载压缩包文件. 其他的source网站 ...

  3. Linux 安装 nginx 安装PCRE库

    PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库.这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是 ...

  4. pcre库

    pcre : perl compatible  regular expressions , perl 兼容正则表达式 www.pcre.org 按装pcre是为了使Nginx支持具备URI重写功能的 ...

  5. Sword pcre库函数学习三

    14.pcre_get_substring_list 原型: #include <pcre.h> int pcre_get_substring_list(const char *subje ...

  6. Sword pcre库函数学习二

    9.pcre_free_substring_list 原型: #include <pcre.h> void pcre_free_substring_list(const char **st ...

  7. Sword pcre库函数学习一

    0.pcre_exec 原型: #include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, con ...

  8. Sword 第三方库介绍二

    /* uuid生成 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <ass ...

  9. Sword 第三方库介绍一

    /* 获取字符编码 */ #include <stdio.h> #include <stdlib.h> /* calloc()函数头文件 */ #include <str ...

随机推荐

  1. HTML常用标签(整理)

    超文本标记语言,即HTML(Hypertext Markup Language),是用于描述网页文档的一种标记语言.现在应用主流是HTML 4.01版本,发布于1999年,为W3C推荐标准.HTML ...

  2. 两条命令在Linux主机之间建立信任关系

    ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa //生成当前用户密钥 ssh-copy-id -i /root/.ssh/id_rsa.pub r ...

  3. Java集合框架:EnumMap

    EnumMap定义 package java.util; import java.util.Map.Entry; import sun.misc.SharedSecrets; public class ...

  4. 浅析Java中的final关键字--转

    转载自:http://www.importnew.com/18586.html#comment-581628 谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关 ...

  5. zabbix 通过自定义key完成网卡监控

    创建执行脚本: # cat /etc/zabbix/monitor_scripts/network.sh #!/bin/bash #set -x usage() { echo "Useage ...

  6. NET Core 1.1 版本项目和2.0环境下的项目开发注意事项

    在NET Core 1.1开发下的项目最好不要随便把工具更新升级到2.0,这样最容易导致之前的.NETCore直接被升级不兼容早前版本 会引起项目无法启动在运行调试IIS express 时候直接一闪 ...

  7. debian8.5安装sublime text3

    在官网www.sublimetext.com下载安装包 我这里用的是Ubuntu 64 bit版. 下载后使用su命令切换到root账户. 执行安装命令 dpkg -i sublime-text*.d ...

  8. iOS 开发之版本上线更新流程

    iOS 开发之版本上线更新流程   把自己app上线的流程记录下来,希望能够让自己加深印象,也能帮助到一些人便是极好的! 证书和描述文件的配置就不写了,直接配置工程吧. 大致把我自己上线的流程写一下: ...

  9. Kali Linux安装Remmina无法加载RDP插件

    原因是确少匹配的 libfreerdp库 可以到这里下载 http://ftp.de.debian.org/debian/pool/main/f/freerdp/ 我的电脑是64位的  我下载的是ht ...

  10. UNIX环境高级编程 apue.h头文件的配置

    http://jimslinbing.blog.163.com/blog/static/85054319201292712414518/ 1.到http://www.apuebook.com下载源码2 ...