1. 程式人生 > >[C/C++11]_[初級]_[使用正則表示式庫進行分組查詢]

[C/C++11]_[初級]_[使用正則表示式庫進行分組查詢]

場景

1.正則表示式在查詢替換字串資料時效率很高, 可以節省很多不必要的查詢程式碼. 特別是對字串分組的查詢, 可以說如果沒有正則表示式,查詢分組裡的字串需要寫很多額外的程式碼,還不一定準確.

2.查詢並替換XML標籤是比較常見的需求, 比如過濾掉HTML標籤裡的標籤, 提取字串內容等.

例子

1.這裡舉例了C++正則庫的分組查詢功能, 一個用於提取特定字串, 一個用於替換字串.


// test-regexp.cpp : 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include <regex>
#include <iostream
>
std::wstring PickLinkShowContent(const wchar_t* buf1){ std::wstring buf(buf1); std::wregex pattern(L"(<A HREF=\"[^\"]*\">)([^<]*)(</A>)", std::regex_constants::ECMAScript|std::regex_constants::icase); auto words_begin = std::wsregex_iterator(buf.begin(), buf.end(), pattern); auto words_end = std::wsregex_iterator(); std::wstring result; int start = 0; int end = 0; for (std::wsregex_iterator i = words_begin; i != words_end; ++i) { std::wsmatch match = *i; size_t pos = match.position(); size_t length = match.length(); std::wstring prev = buf.substr(start,pos-start); result.append(prev); result.append(match[2]); end = pos+length; start = pos+length; } if(end+1 != buf.length()){ result.append(buf.substr(end)); } return result; } std::string ReplaceStr(const char* text,const char* old,const char* _new){ std::regex pattern(old); std::string rep(_new); std::string text1(text); std::string tmp = std::regex_replace(text1,pattern,rep); return tmp; } int _tmain(int argc, _TCHAR* argv[]) { std::cout << "Test
regex group 1" << std::endl; const wchar_t* buf = L"You haven't installed QQPCMgr on this computer. " L"Please <a HREF=\"http://www.qq.com\">
download</A> and install the latest QQPCMgr first, " L"then reboot the program."; auto result = PickLinkShowContent(buf); std::wcout << result
<< std::endl; std::cout << "Test regex group 2" << std::endl; static const char* buf2 = "" "<key>
name</key>" "<string>百度</string><key>position</key>" "<string>2</string>" "</dict>" "<key>05975BCB-FD95-48A4-A912-37ECACD39871</key>" "<dict>" "<key>name</key>" "<string>必應</string>" "<key>url</key>" "<string>http://www.bing.com/</string>" "<key>position</key>" "<string>3</string>"; auto result2 = ReplaceStr(buf2,"(<key>position</key>[^<]*)(<string>)([0-9]*)(</string>)", "$1<integer>$3</integer>"); std::cout << result2 << std::endl; system("pause"); return 0; } ```` 輸出 <div class="se-preview-section-delimiter"></div> ``` XML Test regex group 1 You haven't installed QQPCMgr on this computer. Please download and install the latest QQPCMgr first, then reboot the program. Test regex group 2 <key>name</key><string>百度</string><key>position</key><integer>2</integer></dic t><key>05975BCB-FD95-48A4-A912-37ECACD39871</key><dict><key>name</key><string>必 應</string><key>url</key><string>http://www.bing.com/</string><key>position</key ><integer>3</integer>

參考