[C/C++11]_[初级]_[使用正则表达式库进行分组查询]
2017-05-15 14:07
302 查看
场景
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
相关文章推荐
- ORACLE使用row_number() over(...)对查询数据进行分组并排序
- DataTable、List使用groupby进行分组和分组统计;List、DataTable查询筛选方法
- Excel 中使用SQL 语句查询数据(八)-----用Group by 进行分组统计
- 在entity framework 中使用 LINQ 对表进行左关联查询且group by 分组查询的示例,并且按小时分组查询时间段
- [C/C++11]_[初级]_[使用正则表达式库regex]
- [C/C++11]_[初级]_[使用正则表达式库regex]
- 使用聚合函数group by后面必须要带着所有要查询的列,但是我只想根据group by后面的第一个列进行分组,怎么办? [
- MySQL中使用group by进行分组时,子查询中order by失效的问题
- linq实现两表关联查询,使用group by进行分组
- Access使用查询--1.2.用选择查询进行分组数据的计算
- 在SQL中使用convert函数进行日期的查询的代码
- 使用OTL对oracle数据库进行增加,删除,编辑和查询操作。
- SQL SERVER2000教程-第五章 处理数据 第十六节 使用CHARINDEX函数代替Like进行数据查询
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- hibernate学习笔记(三)----------使用Query进行查询
- Oracle使用SQL语句进行递归查询
- hibernate学习笔记(二)------------使用criteria进行查询
- 使用asp.net进行多关键字查询的例子
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- 使用JNDI进行高级DNS查询