PCRE、PCRE2 以及PCRE++ 使用教程
PCRE、PCRE2 以及PCRE++ 庫的源碼,PCER2 參考我的博客。??
2.VS2008 建立一個win32 程序 ,工程名PcreDemo。
3.將所需的DLL 和 lib 拷貝進工程相應目錄,并進行相關設置
4.? pcre 簡單使用
?pcre? *re;?
?const char *error;?
?int? erroffset;?
?int? ovector[OVECCOUNT];?
?int? rc;
?string strOrg = "I am hk!";
?string strRex = "\\b\\w+\\b";
?printf("前言:以下例子只是非常簡單的應用,僅供參考!\n?? pcreD.dll?? pcre2.dll? pcre++.dll? pcreD.lib?? pcre2.lib? pcre++.lib 請按照文檔網址自行下載并編譯!\n");
?printf("\n\n\n");
?printf("?PCRE 1 庫?? 應用實例:\n\n");
?printf("字符串是 : %s\n", strOrg.c_str());
?printf("正則表達式是: %s\n\n\n", strRex.c_str());
?char *p = (char *) strOrg.c_str();
?re = pcre_compile(strRex.c_str(),?????? // pattern, 輸入參數,將要被編譯的字符串形式的正則表達式,
??PCRE_CASELESS|PCRE_GLOBAL,// 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 false;
?}
?rc = pcre_exec(re, NULL, p, strlen(p), 0, 0, ovector, OVECCOUNT);
?if (rc < 0) {???????????????????? //如果沒有匹配,返回錯誤信息
??if (rc == PCRE_ERROR_NOMATCH)
???printf("Sorry, no match ...\n");
??else
???printf("Matching error %d\n", rc);
??pcre_free(re);
??return false;
?}
?while ( ( rc = pcre_exec(re, NULL, p, strlen(p), 0, 0, ovector, OVECCOUNT)) != PCRE_ERROR_NOMATCH )
?{
??char *substring_start = p + ovector[0];
??int substring_length = ovector[1] - ovector[0];
??char matched[1024];
??memset( matched, 0, 1024 );
??strncpy( matched, substring_start, substring_length );
??//strDes = matched;
??printf( "全部的匹配結果有:%s\n", matched );
??p += ovector[1];?
??if (*p == '\0')
??{
???break;
??}
?}
?pcre_free(re);???????????????????? // 編譯正則表達式re 釋放內存
5 .pcre2 簡單使用
?printf("?PCRE 2 庫? 應用實例:\n\n");
?int error_code = 0;
?PCRE2_SIZE error_offset = 0;
?int nOptions =? 0;?
//?const char *pError;?
?string strOrg2 = "I am hk!";
?string strRex2 = "\\b\\w+\\b";
?printf("字符串是 : %s\n", strOrg2.c_str());
?printf("正則表達式是: %s\n\n\n", strRex2.c_str());
?//going to compile the regular expression pattern, and handle? any errors that are detected
??pcre2_code *recm = pcre2_compile(reinterpret_cast<PCRE2_SPTR>(strRex2.c_str()), PCRE2_ZERO_TERMINATED, 0 , &error_code, &error_offset, NULL);
??//Compilation failed: print the error message and exit
??if (re == NULL)
??{
???PCRE2_UCHAR buffer[256];
???pcre2_get_error_message(error_code, buffer, sizeof(buffer));
???printf("PCRE2 compilation failed at offset %d: %s\n", (int)error_offset,buffer);
???return 1;
??}
?//?Using this function ensures that the block is exactly the right size for the number of capturing parentheses in the pattern.
??pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(recm, NULL);
??int rc2 = 0;
??int start_offset = 0;
??unsigned int match_index = 0;
??rc2= pcre2_match(recm, reinterpret_cast<PCRE2_SPTR>(strOrg2.c_str()), strOrg2.length(), start_offset, 0, match_data, NULL);
??//if(rc < 0)
??//{
??//?
??//}
??//else
??//{
??//?int pos = start_offset;
?
??//?while ((rc = pcre2_match(re, reinterpret_cast<PCRE2_SPTR>(strStruff.c_str()), strStruff.length(), start_offset, 0, match_data, NULL)) > 0)
??//?{
??//??pos = start_offset;
??//?}
??//}
??if (rc2 < 0)
??? {
??? switch(rc2)
???{
???case PCRE2_ERROR_NOMATCH: printf("No match\n"); break;
???/*
???Handle other special cases if you like
???*/
???default: printf("Matching error %d\n", rc2); break;
???}
??? pcre2_match_data_free(match_data);?? /* Release memory used for the match */
??? pcre2_code_free(recm);???????????????? /* data and the compiled pattern. */
??? return 1;
??? }
??//going to compile the regular expression pattern, and handle? any errors that are detected
??while ((rc2 = pcre2_match(recm, reinterpret_cast<PCRE2_SPTR>(strOrg2.c_str()), strOrg2.length(), start_offset, 0, match_data, NULL)) > 0)
??{
????//Match succeded. Get a pointer to the output vector, where string offsets are stored.
????PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
????int i = 0;
????for (i = 0; i < rc2; i++)
????{
?????char *substring_start = (char *)strOrg2.c_str() + 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( "全部匹配結果有? :%s\n",matched );
????}
???start_offset = ovector[2*(i-1) + 1];
???if ( strOrg2[start_offset] == '\0')
???{
????break;
???}
??}
??pcre2_match_data_free(match_data);
??pcre2_code_free(recm);
6.pcre++簡單使用
??printf("?PCRE++庫 應用實例:\n\n");
?/*
???? * define a string with a regular expression
???? */
??printf("官方例子如下:\n");
??? string expression = "(\\b\\w+\\b)";
??? /*
???? * this is the string in which we want to search
???? */
??? string stuff = "I am hk!";
??? cout << "? searching in \"" << stuff << "\" for regex \""
? << expression << "\":" << endl;
??? /*
???? * Create a new Pcre object, search case-insensitive ("i")
???? */
?pcrepp::Pcre reg(expression, "i");
???
??? /*
???? * see if the expression matched
???? */
??? if(reg.search(stuff) == true) {
????? /*
?????? * see if the expression generated any substrings
?????? */
????? if(reg.matches() >= 1) {
?/*
? * print out the number of substrings
? */
?cout << "? generated " << reg.matches() << " substrings:" << endl;
??
?/*
? * iterate over the matched sub strings
? */
?for(int pos=0; pos < reg.matches(); pos++) {
?? /* print out each substring */
?? cout << "? substring " << pos << ": " << reg[pos];?? // also possible: reg.get_match(pos);
?? /* print out the start/end offset of the current substring
??? * within the searched string(stuff)
??? */
?? cout << " (start: " << reg.get_match_start(pos) << ", end: "
??????? << reg.get_match_end(pos) << ")" << endl;
?}
????? }
????? else {
?/*
? * we had a match, but it generated no substrings, for whatever reason
? */
?cout << "?? it matched, but there where no substrings." << endl;
????? }
??? }
??? else {
????? /*
?????? * no match at all
?????? */
????? cout << "?? didn't match." << endl;
??? }
?printf("我自己做相關封裝如下:\n");
?pcrepp::Pcre regg(expression);
?int npos = 0;
?if ( !regg.search(stuff, npos))
?{
??return false;
?}
?while(regg.search(stuff, npos))
?{
??if(regg.matches() >= 1)
??{
???string strMatched = regg.get_match(0);
???printf("全部匹配結果有:%s\n",strMatched.c_str());
???npos = regg.get_match_end(0) + 1;
??}
?}
7. OK!完成啦,有什么問題大家可以給我留言哦!
總結
以上是生活随笔為你收集整理的PCRE、PCRE2 以及PCRE++ 使用教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python离线翻译
- 下一篇: 这么简单,抄抄,改改就行啦