场景
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>
参考
std::regex_replace
使用正则表达式库regex