c语言正则库pcre编译及使用

目录

1. 前置条件

2. 编译pcre2生成静态链接库

 2.1 编译体系及工具链选择

2.2 编译参数配置

3. 库的使用

3.1 库的添加

3.2 头文件的添加

3.3 代码实现

4. 库和头文件分享


1. 前置条件

windows 8 64位机器,已经安装meson 、ninja 、mingw gcc8.1.0版本、cmake。

以上四个工具网上很容易下载到:

1)meson和ninja 在同一个包里面,在meson官网可以下载,目前采用了0.53.1的版本。

2) mingw64 在sourceforge下载

3) cmake 官网下载。

pcre2采用的版本 pcre2-10.34 ,直接从官网下载

2. 编译pcre2生成静态链接库

 2.1 编译体系及工具链选择

命令行下运行cmake-gui,选择编译的源码和build目录,其中build目录为自己新建

D:\meson\test>cmake-gui

选择Configure,弹出 编译体系选项及工具链选择,此处使用ninja编译,本地默认的工具链

工具链探测相关内容:

 The C compiler identification is GNU 8.1.0
Check for working C compiler: C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe
Check for working C compiler: C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Could NOT find BZip2 (missing: BZIP2_LIBRARIES BZIP2_INCLUDE_DIR) 
Found ZLIB: C:/Program Files (x86)/Acer/abFiles/zlib1.dll (found version "1.2.5") 
Could not find OPTIONAL package Readline
Could not find OPTIONAL package Editline
Looking for dirent.h
Looking for dirent.h - found
Looking for stdint.h
Looking for stdint.h - found
Looking for inttypes.h
Looking for inttypes.h - found
Looking for sys/stat.h
Looking for sys/stat.h - found
Looking for sys/types.h
Looking for sys/types.h - found
Looking for unistd.h
Looking for unistd.h - found
Looking for windows.h
Looking for windows.h - found
Looking for bcopy
Looking for bcopy - not found
Looking for memmove
Looking for memmove - found
Looking for strerror
Looking for strerror - found

2.2 编译参数配置

 首先采用默认参数生成静态库。更改参数可以生成动态库。目前采用静态库方式。 

选择编译工具后,会默认选择工具选项,不想更改的话,可以用默认的,如下。

 

而后再次选择Configure,配置编译参数后,选择Generate,即可以生成编译体系。编译参数的内容如下:

Could NOT find BZip2 (missing: BZIP2_LIBRARIES BZIP2_INCLUDE_DIR) 
Could not find OPTIONAL package Readline
Could not find OPTIONAL package Editline


PCRE2 configuration summary:

  Install prefix .................. : C:/Program Files (x86)/PCRE2
  C compiler ...................... : C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe
  C compiler flags ................ :  -ID:/meson/pcre2-10.34/src 

  Build 8 bit PCRE2 library ....... : ON
  Build 16 bit PCRE2 library ...... : OFF
  Build 32 bit PCRE2 library ...... : OFF
  Enable JIT compiling support .... : OFF
  Use SELinux allocator in JIT .... : OFF
  Enable Unicode support .......... : ON
  Newline char/sequence ........... : LF
  \R matches only ANYCRLF ......... : OFF
  \C is disabled .................. : OFF
  EBCDIC coding ................... : OFF
  EBCDIC coding with NL=0x25 ...... : OFF
  Rebuild char tables ............. : OFF
  Internal link size .............. : 2
  Parentheses nest limit .......... : 250
  Heap limit ...................... : 20000000
  Match limit ..................... : 10000000
  Match depth limit ............... : MATCH_LIMIT
  Build shared libs ............... : OFF
  Build static libs ............... : ON
  Build pcre2grep ................. : ON
  Enable JIT in pcre2grep ......... : ON
  Enable callouts in pcre2grep .... : ON
  Enable callout fork in pcre2grep. : ON
  Buffer size for pcre2grep ....... : 20480
  Build tests (implies pcre2test .. : ON
               and pcre2grep)
  Link pcre2grep with libz ........ : ON
  Link pcre2grep with libbz2 ...... : Library not found
  Link pcre2test with libeditline . : Library not found
  Link pcre2test with libreadline . : Library not found
  Support Valgrind .................: OFF
  Use %zu and %td ..................: AUTO

Configuring done
Generating done

 

生成的编译体系如下图:

 

而后运行编译命令,生成库文件和可执行文件

D:\meson\pcre2-10.34>ninja -C build

 

    更改配置参数,可以生成动态库

可以看到多了两个dll了

3. 库的使用

  两种方式,可以直接使用pcre的接口,此外该库也封装了posix兼容的接口

 官方说明为,POSIX 兼容的接口:http://pcre.org/current/doc/html/pcre2posix.html

 我们采用posix兼容的接口,这样代码改动最小。

3.1 库的添加

 本文采用mingw64的编译工具链,将第二章中生成的两个静态库 libpcre2-8.a 和libpcre2-posix.a拷贝到工具链的目录下

当然也可以链接时加库的目录。个人以为拷贝到工具链下更合适

3.2 头文件的添加

将头文件pcre2posix.h添加到工具链的include目录中,实际测试pcre2.h不需要添加。

3.3 代码实现

 经过上述两步,就可以在代码中使用此正则库了:

 1)链接编译配置,增加两个库的链接参数   

meson的build文件内容为

project('test','c')
#regex=files('regcomp.c','regex.c','regex_internal.c','regexec.c')
link_args=['-lpcre2-posix','-lpcre2-8']
executable('hellomeson','hellomeson.c',link_args:link_args)

2)头文件 包含 #include "pcre2posix.h" 

在实际编译时,还存在:

hellomeson@exe/hellomeson.c.obj: In function `matchRegex':
D:\meson\test\build/../hellomeson.c:11: undefined reference to `__imp_pcre2_regc
omp'
D:\meson\test\build/../hellomeson.c:18: undefined reference to `__imp_pcre2_rege
xec'
D:\meson\test\build/../hellomeson.c:28: undefined reference to `__imp_pcre2_regf
ree'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

 

实际链接参数已经添加了相应的库 : LINK_ARGS = "-Wl,--allow-shlib-undefined" "-static" "-Wl,--start-group" "-lpcre2-posix" "-lpcre2-8" "-mconsole"

3) 在源代码开始处增加: #define PCRE2_STATIC

上述链接时报错的解决方法:

在源代码开始处增加: #define PCRE2_STATIC,由于采用了PCRE,这里使用#define PCRE2_STATIC,而非#define PCRE_STATIC,增加后即可编译链接通过。完整的代码如下:

#define PCRE2_STATIC
#include <stdio.h>
#include "pcre2posix.h"

int matchRegex(const char* pattern, const char* userString)
{
    int result = 0;
    regex_t regex;
    int regexInit = pcre2_regcomp(&regex, pattern, REG_EXTENDED);
    if( regexInit )
    {
        //Error print : Compile regex failed
    }
    else
    {
        int reti = pcre2_regexec( &regex, userString, 0, NULL, 0 );
        if( 0 != reti )
        {
            //Error print: match failed! 
            printf("no match\n");
            result=1;
        }
        else
        {
            result = 0;
        }
    }
    pcre2_regfree( &regex );
    return result;

}
int main(void)
{
    int ret=0;
    
    ret=matchRegex("hello","helloworld");
    printf("match ?if 0 (%d)\n",ret);
    printf("==========================\n");
    ret=matchRegex("hehe","helloworld");
    printf("match ?if 0 (%d)\n",ret);
}

4. 库和头文件分享

    便于直接在windows 64位下使用,不用再重新编译。

    https://download.csdn.net/download/proware/12383799

 

附录,一些资料:

https://blog.csdn.net/skylin19840101/article/details/39578069  这个大概收集了一些C正则库的比较

   

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C语言本身并没有内置的正则表达式功能。但是你可以使用第三方来实现正则表达式的功能。在C语言中,常用的正则表达式PCRE(Perl Compatible Regular Expressions)和POSIX正则表达式PCRE是一个功能强大的正则表达式,提供了广泛的正则表达式功能。它支持Perl兼容的语法,并提供了多种函数来进行正则表达式匹配和替换。你可以在PCRE官方网站(https://www.pcre.org/)上找到详细的文档和使用示例。 POSIX正则表达式基于POSIX标准定义了一套正则表达式接口。C语言标准中的`regex.h`头文件提供了对POSIX正则表达式的支持。你可以使用`regcomp()`函数编译正则表达式模式,然后使用`regexec()`函数进行匹配。 下面是一个使用POSIX正则表达式的简单示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <regex.h> int main() { regex_t regex; int ret; // 编译正则表达式模式 ret = regcomp(&regex, "abc.*def", 0); if (ret != 0) { printf("Failed to compile regex\n"); exit(1); } // 进行匹配 ret = regexec(&regex, "abcdef", 0, NULL, 0); if (ret == 0) { printf("Matched\n"); } else if (ret == REG_NOMATCH) { printf("Not matched\n"); } else { printf("Error in matching\n"); } // 释放正则表达式资源 regfree(&regex); return 0; } ``` 上述代码中,`regcomp()`函数用于编译正则表达式模式,`regexec()`函数用于进行匹配。你可以根据需要自定义正则表达式模式,并根据匹配结果进行处理。 希望以上信息能对你有所帮助!如有更多问题,请

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

proware

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值