pcre 使用

时间:2023-03-09 23:31:27
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