42.linux下安装pcre库:编程测试正则表达式

一:pcre简介:

PCRE(Perl Compatible Regular Expressions中文含义:perl语言兼容正则表达式)是一个用C语言编写的正则表达式函数库,由菲利普.海泽(Philip Hazel)编写。PCRE是一个轻量级的函数库,比Boost之类的正则表达式库小得多。PCRE十分易用,同时功能也很强大,性能超过了POSIX正则表达式库和一些经典的正则表达式库。

二:编译:

sudo tar -zxvf pcre-8.39.tar.gz -C .
sudo chown -R aston pcre-8.39
mkdir install_lib
sudo ./configure --prefix=/home/aston/huawei/pcre-8.39/install_lib 
sudo make
sudo make install

三:遇到的问题:

1.报错1:

aston@ubuntu:/mnt/hgfs/share/source_insight/main_135/test_pcre$ ./app.out
./app.out: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

原因:程序找不到pcre相关动态库;

解决: 拷贝pcre相关库到:/usr/local/lib/

aston@ubuntu:~/huawei/pcre-8.39/install_lib/lib$ sudo cp *pcre* /usr/local/lib/ -rf

重新测试运行正常:

aston@ubuntu:/mnt/hgfs/share/source_insight/main_135/test_pcre$ ./app.out good_afmbone..se
result is: 0

四:测试:

1.代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <assert.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "pcre.h"

#include <iostream>
#include <string>

using namespace std;

#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;

        /*
            yidong:134.135.136.137.138.139.150.151.152.157.158.159.187.188,147
            liandong:130.131.132.155.156.185.186
            dianxin: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);
        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);
        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;
}

2.打印:

aston@ubuntu:/mnt/hgfs/share/source_insight/main_135/test_pcre$ ./app.out 
please input your telephone number 
15000732435
String : 15000732435
Pattern_CM: "^1(3[4-9]|5[012789]|8[78])\d{8}$"
Pattern_UN: "^1(3[0-2]|5[56]|8[56])\d{8}$"
Pattern_TC: "^18[09]\d{8}$"
Pattern_CDMA: "^1[35]3\d{8}$"

OK, has matched ...

Pattern_CM: "^1(3[4-9]|5[012789]|8[78])\d{8}$"
String : 15000732435

五:移植到ARM:

1.编译:

cp -rf /mnt/hgfs/share/source_insight/main_135/obs_sdk/pcre-8.39.tar.gz .
sudo tar -zxvf pcre-8.39.tar.gz -C .
sudo chown -R aston pcre-8.39
mkdir install_lib

sudo ./configure --prefix=/home/aston/huawei_linux/pcre-8.39/install_lib --host=arm-linux CC=/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-gcc CXX=/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-g++ 

sudo make
sudo make install

六:移植遇到的问题:

1.报错:

./.libs/libpcre.so: could not read symbols: File in wrong format

原因:找不到C++的交叉编译工具链;

解决:
添加CXX交叉编译工具链:

sudo ./configure --prefix=/home/aston/huawei_linux/pcre-8.39/install_lib --host=arm-linux CC=/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-gcc CXX=/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-g++ 

2.warning提示:

libtool: warning: library '/opt/hisi-linux/x86-arm/arm-hisiv300-linux/bin/../lib/gcc/arm-hisiv300-linux-uclibcgnueabi/4.8.3/../../../../arm-hisiv300-linux-uclibcgnueabi/lib/libstdc++.la' was moved.
libtool: warning: library '/opt/hisi-linux/x86-arm/arm-hisiv300-linux/bin/../lib/gcc/arm-hisiv300-linux-uclibcgnueabi/4.8.3/../../../../arm-hisiv300-linux-uclibcgnueabi/lib/libstdc++.la' was moved.
ls /opt/hisi-linux/x86-arm/arm-hisiv300-linux/bin/../lib/gcc/arm-hisiv300-linux-uclibcgnueabi/4.8.3/../../../../arm-hisiv300-linux-uclibcgnueabi/lib/ -l

解决:可忽略,不影响:
在这里插入图片描述

3.报错:和库不兼容incompatible:

aston@ubuntu:/mnt/hgfs/share/source_insight/main_135/test_pcre_linux$ make
g++ test_pcre2.cpp -g -I./include -L./lib -lpcre -lpcreposix -lpcrecpp -ldl -lpthread -o app.out
/usr/bin/ld: skipping incompatible ./lib/libpcre.so when searching for -lpcre
/usr/bin/ld: skipping incompatible ./lib/libpcre.a when searching for -lpcre

原因:
查看so文件和app.out格式:
发现不一致:
在这里插入图片描述

解决: 编译app.out 时使用交叉编译工具链即可。

aston@ubuntu:/mnt/hgfs/share/source_insight/main_135/test_pcre_linux$ make
arm-hisiv300-linux-g++ test_pcre2.cpp -g -I./include -L./lib -lpcre -lpcreposix -lpcrecpp -ldl -lpthread -o app.out

4.报错:找不到库文件:

./app.out: can't load library 'libpcre.so.1'
/mnt/test # ./app.out 

原因:linux系统中当前无pcre库:

解决:
拷贝pcre相关动态库到/usr/lib即可:

cp *so* /usr/lib/ -rf

/mnt/test # ls /usr/lib/*pcre*
/usr/lib/libpcre.so             /usr/lib/libpcrecpp.so.0.0.1
/usr/lib/libpcre.so.1           /usr/lib/libpcreposix.so
/usr/lib/libpcre.so.1.2.7       /usr/lib/libpcreposix.so.0
/usr/lib/libpcrecpp.so          /usr/lib/libpcreposix.so.0.0.4
/usr/lib/libpcrecpp.so.0
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页