pcre2 10.33版本在windows的VS2013上面编译

https://docs.neurobin.org/jpcre2/

https://github.com/jpcre2/jpcre2

现成的别人封装好的库

https://github.com/kiyolee/pcre2-win-build

https://www.pcre.org/

https://sourceforge.net/projects/pcre/files/pcre2/10.33/

首先下载https://sourceforge.net/projects/pcre/

解压到 D:\ABNF\pcre2-10.33,参考  D:\ABNF\pcre2-10.33\NON-AUTOTOOLS-BUILD 中的cmake

如下图所示:

出现如下图:

打开 D:\ABNF\pcre2-10.33\build\PCRE2.sln,出现如下图所示

注意:会发生一个编译错误,解决办法如下:

pcre2-8工程配置如下图所示: WIN32;_WINDOWS;PCRE2_STATIC;PCRE2_CODE_UNIT_WIDTH=8;HAVE_CONFIG_H;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions)

生成D:\ABNF\pcre2-10.33\build\x64\Debug\pcre2-8d.lib。下面进行调用:用ConsoleApplication1来进行调用,其中ConsoleApplication1的工程设置如下-DEBUG-64位:宏定义:WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions);PCRE2_STATIC;PCRE2_CODE_UNIT_WIDTH=8;HAVE_CONFIG_H;,附加包含目录:D:\ABNF\pcre2-10.33\build,引用库目录:D:\ABNF\pcre2-10.33\build\x64\Debug, 引用库:pcre2-8d.lib;,然后运行,用测试 程序如下:测试程序参考 D:\ABNF\pcre2-10.33\src\pcre2test.c。ConsoleApplication1代码如下:

VS2015的宏定义:_DEBUG;_CONSOLE;%(PreprocessorDefinitions);HAVE_CONFIG_H;PCRE2_STATIC;PCRE2_CODE_UNIT_WIDTH=8;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include <iostream>
#include "pcre2.h"
using namespace std;

#if 0
int main(int argc, char* argv[])
{
	cout << "hello world" << endl;
	getchar();
	return 0;
}
#endif


#if 1
int main(int argc, char* argv[])
{
	///(\d+)\.(\d+)\.(\d+)\.(\d+)/  192.168.2.2
	string s0 = argv[0];
	string s1 = argv[1];
	string s2 = argv[2];
	//string s3 = argv[3];

	cout << "hello world" << endl;

	//(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 = NULL;

	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;

	size_t 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]);
			getchar();
			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");
		getchar();
		return 1;
	}

	/* As pattern and subject are char arguments, they can be straightforwardly
	cast to PCRE2_SPTR as we are working in 8-bit code units. */

	//pattern = (PCRE2_SPTR)argv[i];// "/(\d+)\.(\d+)\.(\d+)\.(\d+)/"
	//pattern = (PCRE2_SPTR)("(\d+)\.(\d+)\.(\d+)\.(\d+)");// "/(\d+)\.(\d+)\.(\d+)\.(\d+)/"
	//subject = (PCRE2_SPTR)argv[i + 1];// "192.168.2.2"
	//pattern = (PCRE2_SPTR)("\\w+");
	pattern = (PCRE2_SPTR)("(\\d+)\.(\\d+)\.(\\d+)\.(\\d+)"); //(?'fenzu0'\\d+)) (?'Word'\w+)) 
	//pattern = (PCRE2_SPTR)("(\?\'fenzu0\'\\d+))\.(\\d+)\.(\\d+)\.(\\d+)");
	subject = (PCRE2_SPTR)("192.168.2.2");// "192.168.2.2"
	subject_length = 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);
		getchar();
		return 1;
	}


	/*************************************************************************
	* If the compilation succeeded, we call PCRE again, in order to do a     *
	* pattern match against the subject string. This does just ONE match. If *
	* further matching is needed, it will be done below. Before running the  *
	* match we must set up a match_data block for holding the result.        *
	*************************************************************************/

	/* Using this function ensures that the block is exactly the right size for
	the number of capturing parentheses in the pattern. */

	match_data = pcre2_match_data_create_from_pattern(re, NULL);

	rc = pcre2_match(
		re,                   /* the compiled pattern */
		subject,              /* the subject string */
		subject_length,       /* the length of the subject */
		0,                    /* start at offset 0 in the subject */
		0,                    /* default options */
		match_data,           /* block for storing the result */
		NULL);                /* use default match context */

	/* Matching failed: handle error cases */

	if (rc < 0)
	{
		switch (rc)
		{
		case PCRE2_ERROR_NOMATCH: printf("No match\n"); break;
			/*
			Handle other special cases if you like
			*/
		default: printf("Matching error %d\n", rc); break;
		}
		pcre2_match_data_free(match_data);   /* Release memory used for the match */
		pcre2_code_free(re);                 /* data and the compiled pattern. */
		getchar();
		return 1;
	}

	/* Match succeded. Get a pointer to the output vector, where string offsets are
	stored. */

	ovector = pcre2_get_ovector_pointer(match_data);
	printf("Match succeeded at offset %d\n", (int)ovector[0]);


	/*************************************************************************
	* We have found the first match within the subject string. If the output *
	* vector wasn't big enough, say so. Then output any substrings that were *
	* captured.                                                              *
	*************************************************************************/

	/* The output vector wasn't big enough. This should not happen, because we used
	pcre2_match_data_create_from_pattern() above. */

	if (rc == 0)
		printf("ovector was not big enough for all the captured substrings\n");

	/* We must guard against patterns such as /(?=.\K)/ that use \K in an assertion
	to set the start of a match later than its end. In this demonstration program,
	we just detect this case and give up. */

	if (ovector[0] > ovector[1])
	{
		printf("\\K was used in an assertion to set the match start after its end.\n"
			"From end to start the match was: %.*s\n", (int)(ovector[0] - ovector[1]),
			(char *)(subject + ovector[1]));
		printf("Run abandoned\n");
		pcre2_match_data_free(match_data);
		pcre2_code_free(re);
		getchar();
		return 1;
	}

	/* Show substrings stored in the output vector by number. Obviously, in a real
	application you might want to do things other than print them. */

	for (i = 0; i < rc; i++)
	{
		PCRE2_SPTR substring_start = subject + ovector[2 * i];
		size_t substring_length = ovector[2 * i + 1] - ovector[2 * i];
		printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start);
	}


	/**************************************************************************
	* That concludes the basic part of this demonstration program. We have    *
	* compiled a pattern, and performed a single match. The code that follows *
	* shows first how to access named substrings, and then how to code for    *
	* repeated matches on the same subject.                                   *
	**************************************************************************/

	/* See if there are any named substrings, and if so, show them by name. First
	we have to extract the count of named parentheses from the pattern. */

	(void)pcre2_pattern_info(
		re,                   /* the compiled pattern */
		PCRE2_INFO_NAMECOUNT, /* get the number of named substrings */
		&namecount);          /* where to put the answer */

	if (namecount == 0) printf("No named substrings\n"); else
	{
		PCRE2_SPTR tabptr;
		printf("Named substrings\n");

		/* Before we can access the substrings, we must extract the table for
		translating names to numbers, and the size of each entry in the table. */

		(void)pcre2_pattern_info(
			re,                       /* the compiled pattern */
			PCRE2_INFO_NAMETABLE,     /* address of the table */
			&name_table);             /* where to put the answer */

		(void)pcre2_pattern_info(
			re,                       /* the compiled pattern */
			PCRE2_INFO_NAMEENTRYSIZE, /* size of each entry in the table */
			&name_entry_size);        /* where to put the answer */

		/* Now we can scan the table and, for each entry, print the number, the name,
		and the substring itself. In the 8-bit library the number is held in two
		bytes, most significant first. */

		tabptr = name_table;
		for (i = 0; i < namecount; i++)
		{
			int n = (tabptr[0] << 8) | tabptr[1];
			printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
				(int)(ovector[2 * n + 1] - ovector[2 * n]), subject + ovector[2 * n]);
			tabptr += name_entry_size;
		}
	}


	/*************************************************************************
	* If the "-g" option was given on the command line, we want to continue  *
	* to search for additional matches in the subject string, in a similar   *
	* way to the /g option in Perl. This turns out to be trickier than you   *
	* might think because of the possibility of matching an empty string.    *
	* What happens is as follows:                                            *
	*                                                                        *
	* If the previous match was NOT for an empty string, we can just start   *
	* the next match at the end of the previous one.                         *
	*                                                                        *
	* If the previous match WAS for an empty string, we can't do that, as it *
	* would lead to an infinite loop. Instead, a call of pcre2_match() is    *
	* made with the PCRE2_NOTEMPTY_ATSTART and PCRE2_ANCHORED flags set. The *
	* first of these tells PCRE2 that an empty string at the start of the    *
	* subject is not a valid match; other possibilities must be tried. The   *
	* second flag restricts PCRE2 to one match attempt at the initial string *
	* position. If this match succeeds, an alternative to the empty string   *
	* match has been found, and we can print it and proceed round the loop,  *
	* advancing by the length of whatever was found. If this match does not  *
	* succeed, we still stay in the loop, advancing by just one character.   *
	* In UTF-8 mode, which can be set by (*UTF) in the pattern, this may be  *
	* more than one byte.                                                    *
	*                                                                        *
	* However, there is a complication concerned with newlines. When the     *
	* newline convention is such that CRLF is a valid newline, we must       *
	* advance by two characters rather than one. The newline convention can  *
	* be set in the regex by (*CR), etc.; if not, we must find the default.  *
	*************************************************************************/

#if 0
	if (!find_all)     /* Check for -g */
	{
		pcre2_match_data_free(match_data);  /* Release the memory that was used */
		pcre2_code_free(re);                /* for the match data and the pattern. */
		getchar();
		return 0;                           /* Exit the program. */
	}
#endif

	/* Before running the loop, check for UTF-8 and whether CRLF is a valid newline
	sequence. First, find the options with which the regex was compiled and extract
	the UTF state. */

	(void)pcre2_pattern_info(re, PCRE2_INFO_ALLOPTIONS, &option_bits);
	utf8 = (option_bits & PCRE2_UTF) != 0;

	/* Now find the newline convention and see whether CRLF is a valid newline
	sequence. */

	(void)pcre2_pattern_info(re, PCRE2_INFO_NEWLINE, &newline);
	crlf_is_newline = newline == PCRE2_NEWLINE_ANY ||
		newline == PCRE2_NEWLINE_CRLF ||
		newline == PCRE2_NEWLINE_ANYCRLF;

	/* Loop for second and subsequent matches */

	for (;;)
	{
		uint32_t options = 0;                   /* Normally no options */
		PCRE2_SIZE start_offset = ovector[1];   /* Start at end of previous match */

		/* If the previous match was for an empty string, we are finished if we are
		at the end of the subject. Otherwise, arrange to run another match at the
		same point to see if a non-empty match can be found. */

		if (ovector[0] == ovector[1])
		{
			if (ovector[0] == subject_length) break;
			options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
		}

		/* If the previous match was not an empty string, there is one tricky case to
		consider. If a pattern contains \K within a lookbehind assertion at the
		start, the end of the matched string can be at the offset where the match
		started. Without special action, this leads to a loop that keeps on matching
		the same substring. We must detect this case and arrange to move the start on
		by one character. The pcre2_get_startchar() function returns the starting
		offset that was passed to pcre2_match(). */

		else
		{
			PCRE2_SIZE startchar = pcre2_get_startchar(match_data);
			if (start_offset <= startchar)
			{
				if (startchar >= subject_length) break;   /* Reached end of subject.   */
				start_offset = startchar + 1;             /* Advance by one character. */
				if (utf8)                                 /* If UTF-8, it may be more  */
				{                                       /*   than one code unit.     */
					for (; start_offset < subject_length; start_offset++)
					if ((subject[start_offset] & 0xc0) != 0x80) break;
				}
			}
		}

		/* Run the next matching operation */

		rc = pcre2_match(
			re,                   /* the compiled pattern */
			subject,              /* the subject string */
			subject_length,       /* the length of the subject */
			start_offset,         /* starting offset in the subject */
			options,              /* options */
			match_data,           /* block for storing the result */
			NULL);                /* use default match context */

		/* This time, a result of NOMATCH isn't an error. If the value in "options"
		is zero, it just means we have found all possible matches, so the loop ends.
		Otherwise, it means we have failed to find a non-empty-string match at a
		point where there was a previous empty-string match. In this case, we do what
		Perl does: advance the matching position by one character, and continue. We
		do this by setting the "end of previous match" offset, because that is picked
		up at the top of the loop as the point at which to start again.

		There are two complications: (a) When CRLF is a valid newline sequence, and
		the current position is just before it, advance by an extra byte. (b)
		Otherwise we must ensure that we skip an entire UTF character if we are in
		UTF mode. */

		if (rc == PCRE2_ERROR_NOMATCH)
		{
			if (options == 0) break;                    /* All matches found */
			ovector[1] = start_offset + 1;              /* Advance one code unit */
			if (crlf_is_newline &&                      /* If CRLF is a newline & */
				start_offset < subject_length - 1 &&    /* we are at CRLF, */
				subject[start_offset] == '\r' &&
				subject[start_offset + 1] == '\n')
				ovector[1] += 1;                          /* Advance by one more. */
			else if (utf8)                              /* Otherwise, ensure we */
			{                                         /* advance a whole UTF-8 */
				while (ovector[1] < subject_length)       /* character. */
				{
					if ((subject[ovector[1]] & 0xc0) != 0x80) break;
					ovector[1] += 1;
				}
			}
			continue;    /* Go round the loop again */
		}

		/* Other matching errors are not recoverable. */

		if (rc < 0)
		{
			printf("Matching error %d\n", rc);
			pcre2_match_data_free(match_data);
			pcre2_code_free(re);
			getchar();
			return 1;
		}

		/* Match succeded */

		printf("\nMatch succeeded again at offset %d\n", (int)ovector[0]);

		/* The match succeeded, but the output vector wasn't big enough. This
		should not happen. */

		if (rc == 0)
			printf("ovector was not big enough for all the captured substrings\n");

		/* We must guard against patterns such as /(?=.\K)/ that use \K in an
		assertion to set the start of a match later than its end. In this
		demonstration program, we just detect this case and give up. */

		if (ovector[0] > ovector[1])
		{
			printf("\\K was used in an assertion to set the match start after its end.\n"
				"From end to start the match was: %.*s\n", (int)(ovector[0] - ovector[1]),
				(char *)(subject + ovector[1]));
			printf("Run abandoned\n");
			pcre2_match_data_free(match_data);
			pcre2_code_free(re);
			getchar();
			return 1;
		}

		/* As before, show substrings stored in the output vector by number, and then
		also any named substrings. */

		for (i = 0; i < rc; i++)
		{
			PCRE2_SPTR substring_start = subject + ovector[2 * i];
			size_t substring_length = ovector[2 * i + 1] - ovector[2 * i];
			printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start);
		}

		if (namecount == 0) printf("No named substrings\n"); else
		{
			PCRE2_SPTR tabptr = name_table;
			printf("Named substrings\n");
			for (i = 0; i < namecount; i++)
			{
				int n = (tabptr[0] << 8) | tabptr[1];
				printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
					(int)(ovector[2 * n + 1] - ovector[2 * n]), subject + ovector[2 * n]);
				tabptr += name_entry_size;
			}
		}
	}      /* End of loop to find second and subsequent matches */

	printf("\n");
	pcre2_match_data_free(match_data);
	pcre2_code_free(re);
	getchar();
	return 0;

	getchar();
	return 0;
}
#endif

其中:调试信息如下 :/(\d+)\.(\d+)\.(\d+)\.(\d+)/  192.168.2.2  。字符集也选空,如下图所示:

-g (\d+)\.(\d+)\.(\d+)\.(\d+) 192.168.2.9.292.268.22.29

OK,

release 64版本:
LIB : PCRE2_STATIC;NDEBUG;_WINDOWS;PCRE2_CODE_UNIT_WIDTH=8;HAVE_CONFIG_H;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;CMAKE_INTDIR="Release";%(PreprocessorDefinitions)
ConsoleApplication1 附加包含目录:D:\ABNF\pcre2-10.33\build    链接器输入:pcre2-8.lib; 链接器目录:D:\ABNF\pcre2-10.33\build\x64\Release\  ,链接器的所有选项:目标计算机:MachineX64 (/MACHINE:X64)

其它的release 32 debug32 版本类似,重点是要有 PCRE2_STATIC;PCRE2_CODE_UNIT_WIDTH=8;HAVE_CONFIG_H; 宏,就OK。

测试的话,对照pcre2demo.c中的代码。

上面证明了代码是正确 的,下面来把所要用到的库给提取出来自己编译一下。

事实证明,自己添加源码的话,需要的图如下所示:

其中最后一副图的应该是不用管的,所有用到的H+CPP都是直接通过CMAKE来编译后的,注意这些源CPP和图中的CPP一样,不能加多哦,不然会报错误的。

然后添加宏:PCRE2_STATIC;PCRE2_CODE_UNIT_WIDTH=8;HAVE_CONFIG_H;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;,然后编译并运行成功,成功图和第一种方法一样。

 

 

 

“基于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、付费专栏及课程。

余额充值