pcre2交叉编译

本文档详细介绍了如何交叉编译pcre2库,并在目标平台上进行正则表达式匹配的测试。首先从官网下载pcre2源码,然后使用指定的编译器和配置选项进行编译和安装。在测试程序中,演示了如何使用pcre2进行匹配操作,包括处理错误、获取匹配结果和处理命名子串。测试程序还展示了如何进行多次匹配以找到所有实例。
摘要由CSDN通过智能技术生成

pcre2交叉编译

1.下载pcre2源码包

​ http://www.pcre.org/

2.编译pcre

tar -zxvf pcre2-10.39.tar.gz			//解压
cd pcre2-10.39/    						//进入目录

./configure CROSS_COMPILE=arm-linux CC=arm-linux-gcc  --host=arm-linux --prefix=/home/pnc120432be01/Desktop/liyuworkspace/arm/pcre		//配置	
make    
make install

注意:

  • –prefix: 是pcre2交叉编译后生成文件的路径。

  1. 将/arm/pcre目录中的文件拷贝到arm机中,将/arm/pcre/lib目下的.so文件拷贝到开发板根目录下的lib子目录下,而后编写测试程序以下:
#define PCRE2_CODE_UNIT_WIDTH 8

#include <stdio.h>
#include <string.h>
#include "pcre2.h"


/**************************************************************************
* Here is the program. The API includes the concept of "contexts" for     *
* setting up unusual interface requirements for compiling and matching,   *
* such as custom memory managers and non-standard newline definitions.    *
* This program does not do any of this, so it makes no use of contexts,   *
* always passing NULL where a context could be given.                     *
**************************************************************************/

int main(int argc, char **argv)
{
   
pcre2_code *re;
PCRE2_SPTR pattern;     /* PCRE2_SPTR is a pointer to unsigned code units of */
PCRE2_SPTR subject;     /* the appropriate width (in this case, 8 bits). */
PCRE2_SPTR name_table;

int crlf_is_newline;
int errornumber;
int find_all;
int i;
int rc;
int utf8;

uint32_t option_bits;
uint32_t namecount;
uint32_t name_entry_size;
uint32_t newline;

PCRE2_SIZE erroroffset;
PCRE2_SIZE *ovector;
PCRE2_SIZE subject_length;

pcre2_match_data *match_data;


/**************************************************************************
* First, sort out the command line. There is only one possible option at  *
* the moment, "-g" to request repeated matching to find all occurrences,  *
* like Perl's /g option. We set the variable find_all to a non-zero value *
* if the -g option is present.                                            *
**************************************************************************/

find_all = 0;
for (i = 1; i < argc; i++)
  {
   
  if (strcmp(argv[i], "-g") == 0) find_all = 1;
  else if (argv[i][0] == '-')
    {
   
    printf("Unrecognised option %s\n", argv[i]);
    return 1;
    }
  else break;
  }

/* After the options, we require exactly two arguments, which are the pattern,
and the subject string. */

if (argc - i != 2)
  {
   
  printf("Exactly two arguments required: a regex and a subject string\n");
  return 1;
  }

/* Pattern and subject are char arguments, so they can be straightforwardly
cast to PCRE2_SPTR because we are working in 8-bit code units. The subject
length is cast to PCRE2_SIZE for completeness, though PCRE2_SIZE is in fact
defined to be size_t. */

pattern = (PCRE2_SPTR)argv[i];
subject = (PCRE2_SPTR)argv[i+1];
subject_length = (PCRE2_SIZE)strlen((char *)subject);


/*************************************************************************
* Now we are going to compile the regular expression pattern, and handle *
* any errors that are detected.                                          *
*************************************************************************/

re = pcre2_compile(
  pattern,               /* the pattern */
  PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */
  0,                     /* default options */
  &errornumber,          /* for error number */
  &erroroffset,          /* for error offset */
  NULL);                 /* use default compile context */

/* Compilation failed: print the error message and exit. */

if (re == NULL)
  {
   
  PCRE2_UCHAR buffer[256];
  pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
  printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset,
    buffer);
  r
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
“基于PCRE2的完全封装+功能扩展正则表达式模块!!!” 关于PCRE2: pcre2是一个超强大的正则表达式库,它与Perl兼容,有众多的程序嵌入了它 比如 PHP、Nginx等 Unicode支持: pcre2有三个库,libpcre2-8、libpcre2-16、libpcre2-32,分别支持 1字节代码单元(UTF-8)、2字节代码单元(UTF-16)、4字节代码单元(UTF-32)。 这三个库我都已经编译并且放入压缩包,模块也实现完全封装全部支持,在普通使用中我们只需要用到 libpcre2-8这个库,如果需要Unicode支持则需要用到libpcre2-16这个库而libpcre2-32为32位代码单元支持,模块也支持,根据需求使用选择库。 模块公开的函数和类: 使用说明: P_正则全局加载链接库:加载全局链接库(载入DLL) P_正则编译表达式:编译一个表达式,如果成功返回表达式句柄 P_正则内容替换:进行匹配和替换 P_正则内容高级替换: 进行匹配和替换,不同于内容替换的是这个功能允许使用 \0 \1 \2这种类型的格式字符串传入,用以匹配 完整表达式捕获、第一个子表达式捕获、第二个子表达式捕获,同理支持最大\99 假设表达式为:(\d+)*(\d+),文本内容为:“100*200”,此处的格式为:“\1 => \2”,则最终替换返回的结果为:“100 => 200” P_正则内容匹配: 此功能用于判断某个文本是否与表达式匹配,匹配成功返回真,否则返回假 P_正则内容搜索: 此函数通过已编译的表达式进行搜索内容,如果成功将返回一个搜索结果指针,如果启用全部搜索则返回一个搜索结果数组指针,如果无匹配返回0 。。。。。不一一叙述了,模块内有注释,不懂可以加下面的群 P_正则表达式类 封装于面向过程为类 P_正则表达式_便捷 与 P_正则表达式类 相同,但更加便捷操作 所有函数名称带W的表示支持 8/16/32 位字符单元模式(使用16位模式即可支持通常的Unicode),普通模式不支持宽文本的函数有备注 关于JIT: pcre2库支持JIT编译表达式, 启用JIT编译在编译时稍微多耗费一些时间,但在匹配时速度快得多,这通常运用于单个模式进行多次匹配时需要 关于命名子表达式: 表达式允许加入‘命名标签’,使用命名标签的格式:(?(子表达式)) 例如表达式:(?( [1-9][0-9]{4,} ))匹配文本:jhbxwe8769933jdhxcn 那么将会匹配到 8769933 ,由于前面命名子表达式为name,则可以使用 P_正则取子匹配文本_从名称(搜索结果,name)来获取到 8769933
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值