Chinaunix首页 | 论坛 | 博客
  • 博客访问: 988116
  • 博文数量: 96
  • 博客积分: 1553
  • 博客等级: 上尉
  • 技术积分: 1871
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-25 14:50
个人简介

专注点,细心点,耐心点 知行合一

文章分类

全部博文(96)

文章存档

2018年(1)

2014年(4)

2013年(31)

2012年(56)

2011年(4)

分类: C/C++

2012-12-06 22:36:20

1 各种版本的pcre下载地址:


2 pcre软件安装(以pcre-7.5为例):
a 下载
b 解压缩
  unzip pcre-7.5.zip
c 安装
  cd pcre-7.5
  ./configure 
  make 
  make install

3 c语言调用:
  /usr/local/include/pcre.h 定义。举例说明:

源码:
#include
#include
#include

#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            buffer[128];
        memset(buffer,'\0',128);
        char            src    [] = "Hello World";
        char            pattern   [] = "(.*)";
        printf("String : %s\n", src);
        printf("Pattern: \"%s\"\n", pattern);
        re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
        if (re == NULL) {
                printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
                return 1;
        }

        rc = pcre_exec(re, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);
        if (rc < 0) {
                if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match ...\n");
                else    printf("Matching error %d\n", rc);
                free(re);
                return 1;
        }
        printf("\nOK, has matched ...\n\n");
       for (i = 0; i < rc; i++)
       {
                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);
        }
        free(re);
        return 0;
}
编译:
gcc -Wall -g -I/usr/local/include -L/usr/local/lib -lpcre -o main main.c
执行结果:
String : Hello World
Pattern: "(.*)"

OK, has matched ...

 0: Hello World
 1: Hello World

附:pcre语法:
/ 定界符
^ 字符串头
$ 字符串尾
[a-z] 所有小写字母
[A-Z] 所有大写字母
[0-9] 所有数字
? 零或一个紧接前的字符
* 零或多个紧接前的字符
+ 一或多个紧接前的字符
{4} 4个紧接前的字符
{4,8} 4-8个紧接前的字符
. 任意字符
(red|green|blue) Red 或 green 或 blue(红 或 绿 或 蓝)
s 空格

特殊字符(需要在前加 )

( ) [ ] . * ? + ^ | $

/////////////////////////////////////////////////////////////////
PCRE函数在C语言中的使用小例子

在使用PCRE库时,首先肯定是需要安装pcre的,不过一般的系统都会有自带的PCRE库。不过如果想使用最新版本的话,也可以自已下载一个安装包。我这里下载的安装是pcre-8.13.tar.gz版本。安装过程很简单,把安装包上传需要安装的服务器上,安装时默认路径即可,我是在linux环境下安装的,执行命令如下:

1.[root@host70-151 pcre-8.13]# ./configure

2.[root@host70-151 pcre-8.13]# make && make install

此两步即可安装完成,安装成功后的头文件在:/usr/local/include, 库文件在:/usr/local/lib 

下面是我的一个使用PCRE库函数的一个小例子,其功能是匹配手机号码的正则表达式是否成功,分成四类手机号码时行匹配,分别是移动、电信、联通和CDMA的手机号。里面用到了PCRE库函数中的pcre_compile()和pcre_exec()

因为我是在linux下编译C程序的,所以要用到makefile文件。注意:如果你在编译时出现提示:

/usr/zej/zej_test/kernel/pcre_test2.c:29: undefined reference to `pcre_compile'

/usr/zej/zej_test/kernel/pcre_test2.c:35: undefined reference to `pcre_exec'

没有定义pcre.h文件里面的函数时,是因为没有链接到库文件里,这时可以能过修改makefile,在l里面添加一个lpcre即可。然后在编译便可成功。

#include 

#include 

#include 

#include 

#define OVECCOUNT 30    /* should be a multiple of 3 */

#define EBUFLEN 128            

#define BUFLEN 1024

int main()

{              

        pcre *reCM, *reUN, *reTC, *reCDMA; 

        const char *error;

        int erroffset;

        int ovector[OVECCOUNT];

        int rcCM, rcUN, rcTC, rcCDMA, i; 

/*        

常用号段前三位

中国移动:134.135.136.137.138.139.150.151.152.157.158.159.187.188 ,147(数据卡)

中国联通:130.131.132.155.156.185.186 

中国电信:133.153.180.189 

CDMA 133,153

*/

       char src[22];  

       char pattern_CM[] = "^1(3[4-9]|5[012789]|8[78])\\d{8}$";

       char pattern_UN[] = "^1(3[0-2]|5[56]|8[56])\\d{8}$";

       char pattern_TC[] = "^18[09]\\d{8}$";

       char pattern_CDMA[] = "^1[35]3\\d{8}$";

      

       printf("please input your telephone number \n");

       scanf("%s", src);

       printf("String : %s\n", src);

       printf("Pattern_CM: \"%s\"\n", pattern_CM);

       printf("Pattern_UN: \"%s\"\n", pattern_UN);

       printf("Pattern_TC: \"%s\"\n", pattern_TC);

       printf("Pattern_CDMA: \"%s\"\n", pattern_CDMA);

       

       reCM = pcre_compile(pattern_CM, 0, &error, &erroffset, NULL);  //将正则表达式编译成pcre内部表示结构

       reUN = pcre_compile(pattern_UN, 0, &error, &erroffset, NULL);

       reTC = pcre_compile(pattern_TC, 0, &error, &erroffset, NULL);

       reCDMA = pcre_compile(pattern_CDMA, 0, &error, &erroffset, NULL);

        

       if (reCM==NULL && reUN==NULL && reTC==NULL && reCDMA==NULL) {

         printf("PCRE compilation telephone failed at offset %d: %s\n", erroffset,  error);

          return 1;        

       }

       rcCM = pcre_exec(reCM, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);  //匹配pcre编译好的模式,成功返回正数,失败返回负数

       rcUN = pcre_exec(reUN, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);

       rcTC = pcre_exec(reTC, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);

       rcCDMA = pcre_exec(reCDMA, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);

       

       if (rcCM<0 && rcUN<0 && rcTC<0 && rcCDMA<0) {      //若没匹配返回错误信息

        if (rcCM==PCRE_ERROR_NOMATCH && rcUN==PCRE_ERROR_NOMATCH &&

         rcTC==PCRE_ERROR_NOMATCH && rcTC==PCRE_ERROR_NOMATCH) {

         printf("Sorry, no match ...\n");        

        }

        else {

         printf("Matching error %d\n", rcCM);

         printf("Matching error %d\n", rcUN);

         printf("Matching error %d\n", rcTC);

         printf("Matching error %d\n", rcCDMA);

        }           

        free(reCM);

        free(reUN);

        free(reTC);

        free(reCDMA);          

        return 1;

       }

       printf("\nOK, has matched ...\n\n");

       if (rcCM > 0) {

        printf("Pattern_CM: \"%s\"\n", pattern_CM);

        printf("String : %s\n", src);

       }

       if (rcUN > 0) {

        printf("Pattern_UN: \"%s\"\n", pattern_UN);  

        printf("String : %s\n", src);     

       }

       if (rcTC > 0) {

        printf("Pattern_TC: \"%s\"\n", pattern_TC);

        printf("String : %s\n", src);

       }    

       if (rcCDMA > 0) {

        printf("Pattern_CDMA: \"%s\"\n", pattern_CDMA);   

        printf("String : %s\n", src);

       }

       free(reCM);        //释放内存

       free(reUN);

       free(reTC);

       free(reCDMA);        

       return 0;

}


[1]:一些正则表达库的对比

[2]:Boost和PCRE正则库的性能对比

[3]:正则表达式语言元素


阅读(2982) | 评论(1) | 转发(2) |
给主人留下些什么吧!~~

nanye19842013-03-09 14:49:18

从楼主分享的和原创的文章看,楼主是做后台的工程师,而且工作3年多的,因为楼主方向很专一,当然这都是我的猜测