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,也不能连互联网.那么我们只能自 ...
随机推荐
- 彼得原理(The Peter Principle)
一. 关于彼得原理(The Peter Principle) 彼得原理(The Peter Principle)是由美国管理学家劳伦斯·彼得(Laurence.J.Peter)根据千百个有关组织中不能 ...
- JS 在open打开的子窗口页面中调用父窗口页面的JS方法
需求的情景如下: 1:做新增或修改等操作的时候打开一个新的浏览器窗口(使用window.open(参数等)方法) 2:在新增或修改等的页面上有返回按钮.重置按钮.保存按钮,对于返回就直接关闭此窗口(使 ...
- Leetcode026. Remove Duplicates from Sorted Array
water class Solution { public: int removeDuplicates(vector<int>& nums) { for(vector<int ...
- Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance
Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance
- ASP.NET的SEO:目录
ASP.NET的SEO:基础知识 ASP.NET的SEO:Global.asax和HttpModule中的RewritePath()方法--友好的URL ASP.NET的SEO:正则表达式 ASP.N ...
- Windows server 2008 R2远程桌面终端连接数的破解
Windows server 2008 R2远程桌面终端连接数的破解 日常工作中,经常需要远程连接到服务器上,然而在公司里,老总.同事都需要连接到服务器上,而默认的服务器系统同时连接的最大连接数只有2 ...
- poj3274 哈希
这题终于让我AC了,其过程之艰辛我不想再回忆了,看了各种代码,一定要注意指针空和非空的问题,再一个要注意边界. #include <stdio.h> #include <string ...
- 网络流量监控shell脚本
网络收发包计数记录在 /proc/net/dev 文件中, 要取得流量, 只需要读取里面的内容两次, 然后相减, 再除以时间间隔即可. #!/bin/bash #Usage1,record in fi ...
- javaSE第二十七天
第二十七天 447 1:反射(理解) 447 (1)类的加载及类加载器 447 (2)反射: 448 A:定义 448 B:获取字节码对象的三种方式 449 (3) ...
- LINUX下网站压力测试工具webbench
wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz cd we ...