# pcre2_example **Repository Path**: gentheaven/pcre2_example ## Basic Information - **Project Name**: pcre2_example - **Description**: 封装 PCRE2 开源库 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-09-09 - **Last Updated**: 2026-03-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # pcre2_example ## 介绍 封装 PCRE2 开源库 The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl 5. PCRE has its own native API, as well as a set of wrapper functions that correspond to the POSIX regular expression API. The PCRE library is free, even for building proprietary software. Visual Studio 2022 ## 接口说明 ### 注意 这个库要求文本是 **UTF8** 格式的。 ### 正则查找 **regex_match** 函数 ```c char default_regex_str[] = \ "[.]*[第卷][0123456789一二三四五六七八九十零〇百千两]*[章回部节集卷].*[.]*"; 在一个文本中,用正则表达式查找匹配的 cnt = regex_match(default_regex_str, buf, size, 1, on_match, NULL); 返回匹配的次数 输入参数: 参数1: 用于匹配的正则表达式 参数2 和 参数3:文本的内容和大小 参数4:值为1,查找所有匹配的;值为0, 只查找第一个匹配的 参数5:输入输出参数 static int on_match(char* head, size_t item_offset, size_t item_len, void* ptr) { char* match_ptr = head + item_offset; char* tail = match_ptr + item_len; char c = *tail; *tail = 0; FWRITE_IGNORE(match_ptr, (int)strlen(match_ptr)); printf("\n"); *tail = c; return 0; } FWRITE_IGNORE 用于打印 UTF-8 字符串。 ``` **原文**: 《我的生活能开挂》作者:打死不放香菜 第1章 开挂的重生  2013年。 ... 第2章 这剧本不科学啊!  桂庙新村 ... **查找结果**: Match succeeded at offset 509 第1章 开挂的重生 Match succeeded again at offset 7062 第2章 这剧本不科学啊! ### 正则替换 **regex_replace** 函数 ```c char* regex_match_str = "\\\\u003e?"; char* regex_replace_str = ">"; ret = regex_replace(content, len, regex_match_str, regex_replace_str, chg_content, chg_len, 0); 参数1和参数2: 本的内容和大小 参数3: 正则表达式,用于匹配 参数4: 正则表达式,用于替换 参数5和参数6: 替换结果,用户负责管理内存 参数7: 一般为0。如果设置为1, 则查找选项支持 PCRE2_SUBSTITUTE_EXTENDED, extended 特性 ``` 替换前 1. More specific rules have higher precedence; log actions increase rule precedence 2. Filter types precedence: HTTP \u003e SSL \u003e Dst Host 3. Filter actions precedence: Divert \u003e Split \u003e Pass \u003e Block (for the same type of rules) 4. Site fields precedence: SNI \u003e CN for SSL filter and Host \u003e URI for HTTP filter \\u003e 替换为 > 替换后 1. More specific rules have higher precedence; log actions increase rule precedence 2. Filter types precedence: HTTP > SSL > Dst Host 3. Filter actions precedence: Divert > Split > Pass > Block (for the same type of rules) 4. Site fields precedence: SNI > CN for SSL filter and Host > URI for HTTP filter ### 接口如下 ```c typedef int (*regex_on_match)(char* head, size_t item_offset, size_t item_len, void* list); /* match 'regex' at str if find_all is 1, find all matches else only find first return match count return negatvie if error */ extern int regex_match(char* regex, char* str, unsigned int str_len, int find_all, regex_on_match on_match, void* list); /* * regex replace * reg_extend_flag = 0, not set PCRE2_SUBSTITUTE_EXTENDED * reg_extend_flag = 1, set PCRE2_SUBSTITUTE_EXTENDED * return match count return negatvie if error */ extern int regex_replace(char* content, unsigned int content_len, char* regex_match, char* regex_replace, char* chg_content, size_t chg_len, int reg_extend_flag); ``` ## 安装教程 PCRE2 版本:10.46 PCRE2 官网: [PCRE - Perl Compatible Regular Expressions](https://www.pcre.org/) PCRE2 GitHub 代码: [github.com](https://github.com/PCRE2Project/pcre2) 编译 PCRE2: cmake, 编译为静态链接库 静态链接库: pcre2-8-static.lib 头文件: pcre2.h ## 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request ## 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)