关于pcre正则表达式库libpcre

gcc 4.8中已经包含了std regex的头文件

可是没有实现,所以链接是失败的

gcc 4.9完整的支持了c++ 11的regex。

在4.9以前,可以寻求boost的regex。

不过,我更熟悉pcre,基于perl的正则表达式的实现。

下载源码,解压

将库配置为交叉编译,只生成静态库,配置pcre代码如下:

#!/bin/sh

export ROOTDIR="${PWD}"
cd pcre-8.36/

export CROSS_COMPILE="arm-linux-androideabi"

export AR=${CROSS_COMPILE}-ar
export AS=${CROSS_COMPILE}-as
export LD=${CROSS_COMPILE}-ld
export RANLIB=${CROSS_COMPILE}-ranlib
export CC=${CROSS_COMPILE}-gcc
export CXX=${CROSS_COMPILE}-g++
export NM=${CROSS_COMPILE}-nm

./configure --prefix=${ROOTDIR}/build/pcre --target=${CROSS_COMPILE} --host=${CROSS_COMPILE} --build=i686-linux --disable-shared

执行make && make install 编译生成目标文件

接下来我们开始使用libpcre库

枚举所有匹配的项,并将结果打印出来,代码如下:

 1 #include "pcre.h"
 2 #include <string>
 3 #include <vector>
 4 #include <iostream>
 5 
 6 int main(void)
 7 {
 8     const int OVECCOUNT = 30; 
 9     std::string src_string = "Saturday=6 and Sunday=7, but some Fridays=5 joke=102 also.";
10     std::string regex_string = "\\w+=\\d+";
11     bool b_case = false;
12 
13     const char* error;
14     int erroffset;
15     int ovector[OVECCOUNT];
16     std::vector<std::string> sfinds;
17     pcre* re = pcre_compile(regex_string.c_str(), b_case ? PCRE_CASELESS : 0,
18             &error, &erroffset, NULL);
19     if(re)
20     {   
21         unsigned int offset = 0; 
22         unsigned int len = src_string.size();
23         const char* str = src_string.c_str();
24         int rc = 0;
25         while(offset < len && (rc = pcre_exec(re, 0, str, len, offset, 0, ovector, OVECCOUNT)) >= 0)
26         {
27             std::cout << "find count: " << rc << std::endl;
28             for(int i = 0; i < rc; ++i)
29             {
30                 int s_len = ovector[2*i+1] - ovector[2*i];
31                 std::string sfind = src_string.substr(ovector[2*i], s_len);
32                 sfinds.push_back(sfind);
33             }
34             offset = ovector[1];
35         }
36         
37         pcre_free(re);
38     }   
39 
40     for(size_t i = 0; i < sfinds.size(); ++i)
41         std::cout << "*" <<sfinds[i] << "* ";
42     std::cout << std::endl;
43 
44     return 0;
45 }

编译运行regex,输出结果为:

find count: 1
find count: 1
find count: 1
find count: 1
*Saturday=6* *Sunday=7* *Fridays=5* *joke=102*

posted @ 2015-07-09 17:02  Jojodru  阅读(2198)  评论(0编辑  收藏  举报