pcre 使用

摘要:
1、主页地址:http://www.pcre.org/下载pcre-7.8.tar.bz22、解压缩:tarxjpfpcre-7.8.tar.bz23、配置:cdpcre-7.8./configure--prefix=/usr/local/pcre-7.8--libdir=/usr/local/lib/pcre--includedir=/usr/local/include/pcreconfigur

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 -lpcrefile.c
对于C程序,编译命令为:gcc -I/usr/local/include/pcre -L/usr/local/lib/pcre -lpcrecppfile.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 使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SQLAlchemy技术文档(中文版)-下ExtJS 4.2 教程-06:服务器代理(proxy)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

移植QT5.6到嵌入式开发板(史上最详细的QT移植教程)

目前网上的大多数 QT 移植教程还都停留在 qt4.8 版本,或者还有更老的 Qtopia ,但是目前 Qt 已经发展到最新的 5.7 版本了,我个人也已经使用了很长一段时间的 qt5.6 for windows ,本文就来介绍一下QT在嵌入式环境的搭建。   移植以到 JZ2440 为例,使用韦老大提供的 ubuntu9.10 虚拟机作为移植环境。当然,...

未能从程序集ESRI.ArcGIS.AddIns.SDK加载任务"ValidateAddInXMLTask"

开发ArcGIS Add-In程序,在编译时要注意Visual Studio与ArcGIS的版本问题。在多年前,使用的是VS2010+ArcGIS10.2组合,没有编译出错。现在ArcGIS版本不变,VS版本升级为2019,如果直接编译VS2010创建的Add-In程序会报错,具体如下: 错误描述 未能从程序集ESRI.ArcGIS.AddIns.SDK...

Linux被中断的系统调用

慢系统调用,指的是可能永远无法返回,从而使进程永远阻塞的系统调用,比如无客户连接时的accept、无输入时的read都属于慢速系统调用。 在Linux中,当阻塞于某个慢系统调用的进程捕获一个信号,则该系统调用就会被中断,转而执行信号处理函数,这就是被中断的系统调用。 然而,当信号处理函数返回时,有可能发生以下的情况: 如果信号处理函数是用signal注册...

.NET程序运行原理及基本概念详解

一、引言 我们知道在Java中有虚拟机,代码运行时虚拟机把Java语言编译成与机器无关的字节码,然后再把字节码编译成机器指令执行,那么在.NET中程序是如何运行的呢? 其实运行原理是一样的,.NET中的虚拟机是CLR(公共语言运行时),无论是C#程序还是VB程序,首先会由CLR编译成与平台无关的中间语言IL,然后由公共语言运行时CLR的 即时编译器JI...

可变参数__VA_ARGS__ 、 va_start、va_arg、valist 简单使用

 1. 调试功能一般会使用到宏+可变参数的方式 1.1  ##__VA_ARGS__      之详细解析 例如: case A. #define my_print1(...)    printf(__VA_ARGS__)    my_print1("i=%d,j=%d ",i,j)  正确打印 case B. #define my_print2(fm...

error: ‘for’ loop initial declarations are only allowed in C99 mode

比如写出下面这段程序: for (int i = 0; i < n; ++i) do_something(); 然后用gcc编译,会报‘for’ loop initial declarations are only allowed in C99 mode的错误。 原因是在循环条件中声明变量,只在C99标准中支持,C90标准不支持。 所以改成: int...