pcre 使用
1、主页地址:http://www.pcre.org/
下载pcre-7.8.tar.bz2
2、解压缩:
tar xjpf pcre-7.8.tar.bz2
3、配置:
cd pcre-7.8
./configure --prefix=/usr/local/pcre-7.8 --libdir=/usr/local/lib/pcre --includedir=/usr/local/include/pcre
configure有许多参数可配,具体参见./configure --help及手册
4、编译:
make
5、安装:
make install
6、检查:
ls /usr/local 检查是否有pcre-7.8目录
ls /usr/local/lib 检查是否有pcre目录
ls /usr/local/include 检查是否有pcre目录
7、将库文件导入cache:
方法1:在/etc/ld.so.conf中加入: /usr/local/lib/pcre,然后运行ldconfig
方法2:在/etc/ld.so.conf.d/下新生成一个文件(或在其中的文件中加入同样内容),文件内容为:
/usr/local/lib/pcre,然后运行ldconfig
8、使用:
使用pcre编写C或C++程序,然后编译。
对于C程序,编译命令为:gcc -I/usr/local/include/pcre -L/usr/local/lib/pcre -lpcre file.c
对于C程序,编译命令为:gcc -I/usr/local/include/pcre -L/usr/local/lib/pcre -lpcrecpp file.cpp
也可用apt直接安装:
apt-cache search pcre 查找pcre
下面只安装pcrecpp
apt-get install libpcre++-dev 安装pcrecpp开发文件
apt-get install libpcre++0 安装pcrecpp库文件
vi main.c
#define PCRE_STATIC // 静态库编译选项
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#define OVECCOUNT 30 /* should be a multiple of 3 */
#define EBUFLEN 128
#define BUFLEN 1024
int main()
{
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
char src [] = "111 <title>Hello World</title> 222"; // 要被用来匹配的字符串
char pattern [] = "<title>(.*)</(tit)le>"; // 将要被编译的字符串形式的正则表达式
printf("String : %s/n", src);
printf("Pattern: %s//n", pattern);
re = pcre_compile(pattern, // pattern, 输入参数,将要被编译的字符串形式的正则表达式
0, // options, 输入参数,用来指定编译时的一些选项
&error, // errptr, 输出参数,用来输出错误信息
&erroffset, // erroffset, 输出参数,pattern中出错位置的偏移量
NULL); // tableptr, 输入参数,用来指定字符表,一般情况用NULL
// 返回值:被编译好的正则表达式的pcre内部表示结构
if (re == NULL) { //如果编译失败,返回错误信息
printf("PCRE compilation failed at offset %d: %s/n", erroffset, error);
return 1;
}
rc = pcre_exec(re, // code, 输入参数,用pcre_compile编译好的正则表达结构的指针
NULL, // extra, 输入参数,用来向pcre_exec传一些额外的数据信息的结构的指针
src, // subject, 输入参数,要被用来匹配的字符串
strlen(src), // length, 输入参数, 要被用来匹配的字符串的指针
0, // startoffset, 输入参数,用来指定subject从什么位置开始被匹配的偏移量
0, // options, 输入参数, 用来指定匹配过程中的一些选项
ovector, // ovector, 输出参数,用来返回匹配位置偏移量的数组
OVECCOUNT); // ovecsize, 输入参数, 用来返回匹配位置偏移量的数组的最大大小
// 返回值:匹配成功返回非负数,没有匹配返回负数
if (rc < 0) { //如果没有匹配,返回错误信息
if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match .../n");
else printf("Matching error %d/n", rc);
pcre_free(re);
return 1;
}
printf("/nOK, has matched .../n/n"); //没有出错,已经匹配
for (i = 0; i < rc; i++) { //分别取出捕获分组 $0整个正则公式 $1第一个()
char *substring_start = src + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("$%2d: %.*s/n", i, substring_length, substring_start);
}
pcre_free(re); // 编译正则表达式re 释放内存
return 0;
}
重点:
/tmp/cciwpVYJ.o: In function `main':
main.c:(.text+0xcc): undefined reference to `pcre_compile'
main.c:(.text+0x136): undefined reference to `pcre_exec'
main.c:(.text+0x17e): undefined reference to `pcre_free'
main.c:(.text+0x232): undefined reference to `pcre_free'
collect2: error: ld returned 1 exit status
原因:
gcc main.c -I/usr/local/include/pcre -L/usr/local/lib/pcre -lpcre
pcre 使用的更多相关文章
- 在C语言中利用PCRE实现正则表达式
1. PCRE简介 2. 正则表达式定义 3. PCRE正则表达式的定义 4. PCRE的函数简介 5. 使用PCRE在C语言中实现正则表达式的解析 6. PCRE函数在C语言中的使用小例子 1. P ...
- [正则表达式]PCRE环视功能
设想一下这个问题,假设为了方便长串数字的阅读性,需要为其添加逗号作为分隔,需要怎么做呢? 2569836495 => 2,569,836,495 正则表达式的匹配通常是从左往右的,这导致无法使用 ...
- [正则表达式]PCRE反向分组引用
在常见的文本匹配场景上,经常会需要用到一些像HTML这样的嵌套标签类型的文本匹配,经过多翻折腾,拼凑出了这样的一条语句 (<([\w]+)>((?1)|[\w\s])*</\2> ...
- centos6.5 升级安装pcre 8.39版本
1.查看系统pcre安装情况 rpm -qa pcre 2.卸载系统自带的旧版本 rpm -e --nodeps pcre 3.下载新版安装 地址:ftp://ftp.csx.cam.ac.uk/pu ...
- ngixn编译安装时,pcre的处理
nginx编译时pcre会提示找不到libpcre.so.1 ./configure --hlep --without-pcre disable PCRE library usage 不使用pcr ...
- PCRE Perl Compatible Regular Expressions Learning
catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...
- Linux下编译安装PCRE库
备注:如果没有root权限,使用 --prefix 指定安装路径 ./configure --prefix=/home/work/tools/pcre-8.xx =================== ...
- PCRE安装
PCRE(Perl Compatible Regular Expressions)是一个轻量级的Perl函数库,包括 perl 兼容的正则表达式库.它比Boost之类的正则表达式库小得多.PCRE十分 ...
- ningx配置ModSecurity重启出现兼容性问题:ModSecurity: Loaded PCRE do not match with compiled!的解决方法
nginx开启错误日志,然后重启nginx,出现如下信息: 2016/12/03 09:40:38 [notice] 18858#0: ModSecurity for nginx (STABLE)/2 ...
- nginx安装pcre
一.有的服务器上没有安装pcre那么安装nginx的时候会报错 所以在安装之前我们可以: yum install pcre-devel 如果很不巧,服务器也没有配yum,也不能连互联网.那么我们只能自 ...
随机推荐
- OS版本调研
1引言 1.1 编写目的 本文的主要目的是通过对当前项目中使用的各种版本的操作系统进行比较,分析各自特性和稳定程度,最终推荐合适的版本作为当前的标准系统. 1.2 背景 当前,部门负责管理维护的现网使 ...
- CLRS:sorting in linear time O(n)
//intput array A,output array result. count array count . //all the elements is in te range of 0~k ...
- flask-admin
初始化 class Admin(app=None, name=None, url=None, subdomain=None, index_view=None, translations_path=No ...
- 尝试自己建立以alpine 为基础的docker基础镜像和组件镜像
安装ubuntu14.04 然后 #获取root权限 sudo su #安装docker apt-get install docker #准备基础镜像 docker pull alpine docke ...
- Linux:/bin/bash和/bin/sh的区别
bash.dash(/bin/bash和/bin/sh) 原文:http://www.cnblogs.com/dkblog/archive/2011/04/02/2003822.html Linux中 ...
- hbase基本结构
HBASE 基本结构一.overview1. hbase <=> NOSQL 不错,hbase 就是某种类型的nosql 数据库,唯一的区别就是他支持海量的数据. hbas ...
- c programming language ___ 5_2.c
#include <stdio.h> #include <ctype.h> #define BUG printf("here!bug!\n"); int g ...
- Ajax的利弊
ajax的优点 1.最大的一点是页面无刷新,在页面内与服务器通信,给用户的体验非常好. 2.使用异步方式与服务器通信,不需要打断用户的操作,具有更加迅速的响应能力. 3.可以把以前一些服务器负担的工 ...
- vue.js插件使用(01) vue-resource
本文的主要内容如下: 介绍vue-resource的特点 介绍vue-resource的基本使用方法 基于this.$http的增删查改示例 基于this.$resource的增删查改示例 基于int ...
- text-rendering 详解
原文链接:http://www.feeldesignstudio.com/2013/05/text-rendering Text-rendering 属性是一个非标准属性,主要用来告诉渲染引擎(ren ...