簡介
PCRE是一個用C語言編寫的正則表達式函數庫,專門解決C語言中使用正則表達式的問題。
環境搭建
首先要下載PCRE源碼:
PCRE下載鏈接
裏面有各個PCRE版本。我下載的是8.37版的。下載後,要對源代碼進行編譯。我是在windows的環境下使用vs2013對PCRE進行動態編譯。編譯生成的lib和dll文件我已經上傳到github,下面是鏈接:
我的編譯好的鏈接庫以及PCRE源碼
大家可以直接用我的鏈接庫,也可也自己嘗試一下,我自己編譯時參考:PCRE編譯參考
大家也可也下載我的工程參考裏面的文件添加與環境的設置。
需要調用PCRE時,只需要往工程裏面添加pcre.h、pcre.dll和pcre.lib三個文件:
這樣就可以在C語言下使用正則表達式了。
例程
安裝完成後,我不同正則表達式應該怎麼編寫,PCRE應該怎麼使用,就啥折騰了一下,下面是代碼,初步體驗一下PCRE的作用。
#define Main_3
/*Main_2和Main_3纔是合理的用法*/
#ifdef Main_0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pcre.h"
#define OVECCOUNT 30 /* should be a multiple of 3 */
#define EBUFLEN 128
#define BUFLEN 1024
int main()
{
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
char src[] = "hi jode,you a guy. HI boy, hi bibi.";
char pattern[] = "(\\bhi\\b)"; //正則表達式
printf("Pattern: \"%s\"\n", pattern);
printf("Src: \"%s\"\n", src);
re = pcre_compile(pattern, 0, &error, &erroffset, NULL); //將正則表達式編譯成pcre內部表示結構
if (re == NULL) {
printf("PCRE compilation telephone failed at offset %d: %s\n", erroffset, error);
getchar();
return 1;
}
rc = pcre_exec(re, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT); //匹配pcre編譯好的模式,成功返回正數,失敗返回負數
if (rc<0) { //若沒匹配返回錯誤信息
if (rc == PCRE_ERROR_NOMATCH) {
printf("Sorry, no match ...\n");
}
else {
printf("Matching error %d\n", rc);
}
free(re);
getchar();
return 1;
}
printf("\nOK, has matched ...\n\n"); //匹配成功把對應的正則表達式和號碼打印出來
printf("\nRC:%d\n\n", rc); //沒有出錯,已經匹配
if (rc > 0) {
printf("Pattern: \"%s\"\n", pattern);
printf("String : %s\n", src);
}
for (i = 0; i < rc; ++i)
{
char *substring_start = src + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("$%2d: %.*s\n", i, substring_length, substring_start); // %.*s 對應寬度+開始 兩個參數
}
free(re); //釋放內存
getchar();
return 0;
}
#endif
#ifdef Main_1
#include <stdio.h>
#include <string.h>
#include "pcre.h"
#define OVECCOUNT 30 /* should be a multiple of 3 */
#define EBUFLEN 128
#define BUFLEN 1024
int main()
{
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
char src [] = "111 <title>Hello World</title> 222"; // 要被用來匹配的字符串
// char pattern [] = "<title>(.*)</(tit)le>"; // 將要被編譯的字符串形式的正則表達式
char pattern [] = "\\btitle";
printf("String : %s\n", src);
printf("Pattern: \"%s\"\n", pattern);
re = pcre_compile(pattern, // pattern, 輸入參數,將要被編譯的字符串形式的正則表達式
0, // options, 輸入參數,用來指定編譯時的一些選項
&error, // errptr, 輸出參數,用來輸出錯誤信息
&erroffset, // erroffset, 輸出參數,pattern中出錯位置的偏移量
NULL); // tableptr, 輸入參數,用來指定字符表,一般情況用NULL
// 返回值:被編譯好的正則表達式的pcre內部表示結構
if (re == NULL) { //如果編譯失敗,返回錯誤信息
printf("PCRE compilation failed at offset %d: %s/n", erroffset, error);
getchar();
return 1;
}
rc = pcre_exec(re, // code, 輸入參數,用pcre_compile編譯好的正則表達結構的指針
NULL, // extra, 輸入參數,用來向pcre_exec傳一些額外的數據信息的結構的指針
src, // subject, 輸入參數,要被用來匹配的字符串
strlen(src), // length, 輸入參數, 要被用來匹配的字符串的指針
0, // startoffset, 輸入參數,用來指定subject從什麼位置開始被匹配的偏移量
0, // options, 輸入參數, 用來指定匹配過程中的一些選項
ovector, // ovector, 輸出參數,用來返回匹配位置偏移量的數組
OVECCOUNT); // ovecsize, 輸入參數, 用來返回匹配位置偏移量的數組的最大大小
// 返回值:匹配成功返回非負數,沒有匹配返回負數
if (rc < 0) { //如果沒有匹配,返回錯誤信息
if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match ...\n");
else printf("Matching error %d\n", rc);
pcre_free(re);
getchar();
return 1;
}
printf("\nOK, has matched ...\n\n"); //沒有出錯,已經匹配
printf("\nRC:%d\n\n", rc); //沒有出錯,已經匹配
for (i = 0; i < rc; i++) { //分別取出捕獲分組 $0整個正則公式 $1第一個()
char *substring_start = src + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
printf("$%2d: %.*s\n", i, substring_length, substring_start); // %.*s 對應寬度+開始 兩個參數
}
pcre_free(re); // 編譯正則表達式re 釋放內存
getchar();
return 0;
}
#endif
#ifdef Main_2
#include <stdio.h>
#include <string.h>
#include "pcre.h"
#define OVECCOUNT 30/* should be a multiple of 3 */
#define EBUFLEN 128
#define BUFLEN 1024
int main()
{
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
char src[] = "123.123.123.123:80|1.1.1.1:88|225.225.225.225:8888";
char pattern[] = "(\\d*.\\d*.\\d*.\\d*):(\\d*)";
printf("String : %s\n", src);
printf("Pattern: \"%s\"\n", pattern);
re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
return 1;
}
char *p = src;
while ( ( rc = pcre_exec(re, NULL, p, strlen(p), 0, 0, ovector, OVECCOUNT)) != PCRE_ERROR_NOMATCH )
{
printf("\nOK, has matched ...\n\n");
for (i = 0; i < rc; i++)
{
char *substring_start = p + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
char matched[1024];
memset( matched, 0, 1024 );
strncpy( matched, substring_start, substring_length );
printf( "match:%s\n", matched );
}
p += ovector[1];
if ( !p )
{
break;
}
}
pcre_free(re);
getchar();
return 0;
}
#endif
#ifdef Main_3
#include <stdio.h>
#include <string.h>
#include "pcre.h"
#define OVECCOUNT 30/* should be a multiple of 3 */
#define EBUFLEN 128
#define BUFLEN 1024
int main()
{
pcre *re;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rc, i;
char src[] = "hi guy,stupy.hi papa.hi mama! hi hi hi";
char pattern[] = "(\\bhi)";
printf("String : %s\n", src);
printf("Pattern: \"%s\"\n", pattern);
re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
return 1;
}
char *p = src;
while ( ( rc = pcre_exec(re, NULL, p, strlen(p), 0, 0, ovector, OVECCOUNT)) != PCRE_ERROR_NOMATCH ) {
printf("\nOK, has matched ...\n\n")
for (i = 0; i < rc; i++) {
char *substring_start = p + ovector[2*i];
int substring_length = ovector[2*i+1] - ovector[2*i];
char matched[1024];
memset( matched, 0, 1024 );
strncpy( matched, substring_start, substring_length );
printf( "match:%s\n", matched );
}
p += ovector[1];
if ( !p ) {
break;
}
}
pcre_free(re);
getchar();
return 0;
}
#endif
這裏一共有四個代碼,可以改變宏定義#define Main_3 or 2 or 1 or 0
來看看效果。