PCRE是一個NFA正則引擎,不然不能提供完全與Perl一致的正則語法功能。但它同時也實現了DFA,只是滿足數學意義上的正則。
PCRE提供了19個接口函數,爲了簡單介紹,使用PCRE內帶的測試程序(pcretest.c)示例用法。
1. pcre_compile
原型:
#include <pcre.h>
pcre *pcre_compile(const char *pattern, int options, const char **errptr, int *erroffset, const unsigned char *tableptr);
功能:將一個正則表達式編譯成一個內部表示,在匹配多個字符串時,可以加速匹配。其同pcre_compile2功能一樣只是缺少一個參數errorcodeptr。
參數:
pattern 正則表達式
options 爲0,或者其他參數選項
errptr 出錯消息
erroffset 出錯位置
tableptr 指向一個字符數組的指針,可以設置爲空NULL
示例:
L1720 re = pcre_compile((char *)p, options, &error, &erroroffset, tables);
2. pcre_compile2
原型:
#include <pcre.h>
pcre *pcre_compile2(const char *pattern, int options, int *errorcodeptr, const char **errptr, int *erroffset, const unsigned char *tableptr);
功能:將一個正則表達式編譯成一個內部表示,在匹配多個字符串時,可以加速匹配。其同pcre_compile功能一樣只是多一個參數errorcodeptr。
參數:
pattern 正則表達式
options 爲0,或者其他參數選項
errorcodeptr 存放出錯碼
errptr 出錯消息
erroffset 出錯位置
tableptr 指向一個字符數組的指針,可以設置爲空NULL
3. pcre_config
原型:
#include <pcre.h>
int pcre_config(int what, void *where);
功能:查詢當前PCRE版本中使用的選項信息。
參數:
what 選項名
where 存儲結果的位置
示例:
Line1312 (void)pcre_config(PCRE_CONFIG_POSIX_MALLOC_THRESHOLD, &rc);
4. pcre_copy_named_substring
原型:
#include <pcre.h>
int pcre_copy_named_substring(const pcre *code, const char *subject, int *ovector, int stringcount, const char *stringname, char *buffer, int buffersize);
功能:根據名字獲取捕獲的字串。
參數:
code 成功匹配的模式
subject 匹配的串
ovector pcre_exec() 使用的偏移向量
stringcount pcre_exec()的返回值
stringname 捕獲字串的名字
buffer 用來存儲的緩衝區
buffersize 緩衝區大小
示例:
Line2730 int rc = pcre_copy_named_substring(re, (char *)bptr, use_offsets,
count, (char *)copynamesptr, copybuffer, sizeof(copybuffer));
5. pcre_copy_substring
原型:
#include <pcre.h>
int pcre_copy_substring(const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int buffersize);
功能:根據編號獲取捕獲的字串。
參數:
code 成功匹配的模式
subject 匹配的串
ovector pcre_exec() 使用的偏移向量
stringcount pcre_exec()的返回值
stringnumber 捕獲字串編號
buffer 用來存儲的緩衝區
buffersize 緩衝區大小
示例:
Line2730 int rc = pcre_copy_substring((char *)bptr, use_offsets, count,
i, copybuffer, sizeof(copybuffer));
6. pcre_dfa_exec
原型:
#include <pcre.h>
int pcre_dfa_exec(const pcre *code, const pcre_extra *extra, const char *subject, int length, int startoffset, int options, int *ovector, int ovecsize, int *workspace, int wscount);
功能:使用編譯好的模式進行匹配,採用的是一種非傳統的方法DFA,只是對匹配串掃描一次(與Perl不兼容)。
參數:
code 編譯好的模式
extra 指向一個pcre_extra結構體,可以爲NULL
subject 需要匹配的字符串
length 匹配的字符串長度(Byte)
startoffset 匹配的開始位置
options 選項位
ovector 指向一個結果的整型數組
ovecsize 數組大小
workspace 一個工作區數組
wscount 數組大小
示例:
Line2730 count = pcre_dfa_exec(re, extra, (char *)bptr, len, start_offset,
options | g_notempty, use_offsets, use_size_offsets, workspace,
sizeof(workspace)/sizeof(int));
7. int pcre_exec
原型:
#include <pcre.h>
int pcre_exec(const pcre *code, const pcre_extra *extra, const char *subject, int length, int startoffset, int options, int *ovector, int ovecsize);
功能:使用編譯好的模式進行匹配,採用與Perl相似的算法,返回匹配串的偏移位置。。
參數:
code 編譯好的模式
extra 指向一個pcre_extra結構體,可以爲NULL
subject 需要匹配的字符串
length 匹配的字符串長度(Byte)
startoffset 匹配的開始位置
options 選項位
ovector 指向一個結果的整型數組
ovecsize 數組大小
8. pcre_free_substring
原型:
#include <pcre.h>
void pcre_free_substring(const char *stringptr);
功能:釋放pcre_get_substring()和pcre_get_named_substring()申請的內存空間。
參數:
stringptr 指向字符串的指針
示例:
Line2730 const char *substring;
int rc = pcre_get_substring((char *)bptr, use_offsets, count,
i, &substring);
……
pcre_free_substring(substring);
9. pcre_free_substring_list
原型:
#include <pcre.h>
void pcre_free_substring_list(const char **stringptr);
功能:釋放由pcre_get_substring_list申請的內存空間。
參數:
stringptr 指向字符串數組的指針
示例:
Line2773 const char **stringlist;
int rc = pcre_get_substring_list((char *)bptr, use_offsets, count,
……
pcre_free_substring_list(stringlist);
10. pcre_fullinfo
原型:
#include <pcre.h>
int pcre_fullinfo(const pcre *code, const pcre_extra *extra, int what, void *where);
功能:返回編譯出來的模式的信息。
參數:
code 編譯好的模式
extra pcre_study()的返回值,或者NULL
what 什麼信息
where 存儲位置
示例:
Line997 if ((rc = pcre_fullinfo(re, study, option, ptr)) < 0)
fprintf(outfile, "Error %d from pcre_fullinfo(%d)/n", rc, option);
}
11. pcre_get_named_substring
原型:
#include <pcre.h>
int pcre_get_named_substring(const pcre *code, const char *subject, int *ovector, int stringcount, const char *stringname, const char **stringptr);
功能:根據編號獲取捕獲的字串。
參數:
code 成功匹配的模式
subject 匹配的串
ovector pcre_exec() 使用的偏移向量
stringcount pcre_exec()的返回值
stringname 捕獲字串的名字
stringptr 存放結果的字符串指針
示例:
Line2759 const char *substring;
int rc = pcre_get_named_substring(re, (char *)bptr, use_offsets,
count, (char *)getnamesptr, &substring);
12. pcre_get_stringnumber
原型:
#include <pcre.h>
int pcre_get_stringnumber(const pcre *code, const char *name);
功能:根據命名捕獲的名字獲取對應的編號。
參數:
code 成功匹配的模式
name 捕獲名字
13. pcre_get_substring
原型:
#include <pcre.h>
int pcre_get_substring(const char *subject, int *ovector, int stringcount, int stringnumber, const char **stringptr);
功能:獲取匹配的子串。
參數:
subject 成功匹配的串
ovector pcre_exec() 使用的偏移向量
stringcount pcre_exec()的返回值
stringnumber 獲取的字符串編號
stringptr 字符串指針
14. pcre_get_substring_list
原型:
#include <pcre.h>
int pcre_get_substring_list(const char *subject, int *ovector, int stringcount, const char ***listptr);
功能:獲取匹配的所有子串。
參數:
subject 成功匹配的串
ovector pcre_exec() 使用的偏移向量
stringcount pcre_exec()的返回值
listptr 字符串列表的指針
15. pcre_info
原型:
#include <pcre.h>
int pcre_info(const pcre *code, int *optptr, int *firstcharptr);
已過時,使用pcre_fullinfo替代。
16. pcre_maketables
原型:
#include <pcre.h>
const unsigned char *pcre_maketables(void);
功能:生成一個字符表,表中每一個元素的值不大於256,可以用它傳給pcre_compile()替換掉內建的字符表。
參數:
示例:
Line2759 tables = pcre_maketables();
17. pcre_refcount
原型:
#include <pcre.h>
int pcre_refcount(pcre *code, int adjust);
功能:編譯模式的引用計數。
參數:
code 已編譯的模式
adjust 調整的引用計數值
18. pcre_study
原型:
#include <pcre.h>
pcre_extra *pcre_study(const pcre *code, int options, const char **errptr);
功能:對編譯的模式進行學習,提取可以加速匹配過程的信息。
參數:
code 已編譯的模式
options 選項
errptr 出錯消息
示例:
Line1797 extra = pcre_study(re, study_options, &error);
19. pcre_version
原型:
#include <pcre.h>
char *pcre_version(void);
功能:返回PCRE的版本信息。
參數:
示例:
Line1384 if (!quiet) fprintf(outfile, "PCRE version %s/n/n", pcre_version());
其中用得最多的是以下的函數:
1.pcre_compile
函數原型:
pcre *pcre_compile(const char *pattern, int options, const char **errptr, int *erroffset, const unsigned char *tableptr)
功能:將一個正則表達式編譯成一個內部表示,在匹配多個字符串時,可以加速匹配。其同pcre_compile2功能一樣只是缺少一個參數errorcodeptr。
參數說明:
pattern 正則表達式
options 爲0,或者其他參數選項
errptr 出錯消息
erroffset 出錯位置
tableptr 指向一個字符數組的指針,可以設置爲空NULL。
2. pcre_compile2
函數原型:
pcre *pcre_compile2(const char *pattern, int options, int *errorcodeptr, const char **errptr, int *erroffset, const unsigned char *tableptr)
功能:將一個正則表達式編譯成一個內部表示,在匹配多個字符串時,可以加速匹配。其同pcre_compile功能一樣只是多一個參數errorcodeptr。
參數:
pattern 正則表達式
options 爲0,或者其他參數選項
errorcodeptr 存放出錯碼
errptr 出錯消息
erroffset 出錯位置
tableptr 指向一個字符數組的指針,可以設置爲空NULL。
3. pcre_exec
函數原型:
int pcre_exec(const pcre *code, const pcre_extra *extra, const char *subject, int length, int startoffset, int options, int *ovector, int ovecsize)
功能:使用編譯好的模式進行匹配,採用與Perl相似的算法,返回匹配串的偏移位置。
參數:
code 編譯好的模式
extra 指向一個pcre_extra結構體,可以爲NULL
subject 需要匹配的字符串
length 匹配的字符串長度(Byte)
startoffset 匹配的開始位置
options 選項位
ovector 指向一個結果的整型數組
ovecsize 數組大小。
4. pcre_study
函數原型:
pcre_extra *pcre_study(const pcre *code, int options, const char **errptr)
功能:對編譯的模式進行學習,提取可以加速匹配過程的信息。
參數:
code 已編譯的模式
options 選項
errptr 出錯消息
例子:
#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 *reCM, *reUN, *reTC, *reCDMA;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int rcCM, rcUN, rcTC, rcCDMA, i;
/*
常用號段前三位
中國移動CM:134.135.136.137.138.139.150.151.152.157.158.159.187.188 ,147(數據卡)
中國聯通UN:130.131.132.155.156.185.186
中國電信TC:133.153.180.189
CDMA :133,153
*/
char src[22];
char pattern_CM[] = "^1(3[4-9]|5[012789]|8[78])\\d{8}$"; //正則表達式
char pattern_UN[] = "^1(3[0-2]|5[56]|8[56])\\d{8}$";
char pattern_TC[] = "^18[09]\\d{8}$";
char pattern_CDMA[] = "^1[35]3\\d{8}$";
printf("Please input your telephone number \n");
scanf("%s", src);
printf("String : %s\n", src);
printf("Pattern_CM: \"%s\"\n", pattern_CM);
printf("Pattern_UN: \"%s\"\n", pattern_UN);
printf("Pattern_TC: \"%s\"\n", pattern_TC);
printf("Pattern_CDMA: \"%s\"\n", pattern_CDMA);
reCM = pcre_compile(pattern_CM, 0, &error, &erroffset, NULL); //將正則表達式編譯成pcre內部表示結構
reUN = pcre_compile(pattern_UN, 0, &error, &erroffset, NULL);
reTC = pcre_compile(pattern_TC, 0, &error, &erroffset, NULL);
reCDMA = pcre_compile(pattern_CDMA, 0, &error, &erroffset, NULL);
if (reCM==NULL && reUN==NULL && reTC==NULL && reCDMA==NULL) {
printf("PCRE compilation telephone failed at offset %d: %s\n", erroffset, error);
return 1;
}
rcCM = pcre_exec(reCM, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT); //匹配pcre編譯好的模式,成功返回正數,失敗返回負數
rcUN = pcre_exec(reUN, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);
rcTC = pcre_exec(reTC, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);
rcCDMA = pcre_exec(reCDMA, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);
if (rcCM<0 && rcUN<0 && rcTC<0 && rcCDMA<0) { //若沒匹配返回錯誤信息
if (rcCM==PCRE_ERROR_NOMATCH && rcUN==PCRE_ERROR_NOMATCH &&
rcTC==PCRE_ERROR_NOMATCH && rcTC==PCRE_ERROR_NOMATCH) {
printf("Sorry, no match ...\n");
}
else {
printf("Matching error %d\n", rcCM);
printf("Matching error %d\n", rcUN);
printf("Matching error %d\n", rcTC);
printf("Matching error %d\n", rcCDMA);
}
free(reCM);
free(reUN);
free(reTC);
free(reCDMA);
return 1;
}
printf("\nOK, has matched ...\n\n"); //匹配成功把對應的正則表達式和號碼打印出來
if (rcCM > 0) {
printf("Pattern_CM: \"%s\"\n", pattern_CM);
printf("String : %s\n", src);
}
if (rcUN > 0) {
printf("Pattern_UN: \"%s\"\n", pattern_UN);
printf("String : %s\n", src);
}
if (rcTC > 0) {
printf("Pattern_TC: \"%s\"\n", pattern_TC);
printf("String : %s\n", src);
}
if (rcCDMA > 0) {
printf("Pattern_CDMA: \"%s\"\n", pattern_CDMA);
printf("String : %s\n", src);
}
free(reCM); //釋放內存
free(reUN);
free(reTC);
free(reCDMA);
return 0;
}
#define PCRE_STATIC // 靜態庫編譯選項
#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>"; // 將要被編譯的字符串形式的正則表達式
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);
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);
return 1;
}
printf("/nOK, has matched .../n/n"); //沒有出錯,已經匹配
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);
}
pcre_free(re); // 編譯正則表達式re 釋放內存
return 0;
}
gcc -Wall -lpcre -o test test.c