Regex
来源:互联网 发布:老炮儿网络剧哪看 编辑:程序博客网 时间:2024/06/02 16:39
#include <iostream>#include <cassert>#include <string>#include "boost/regex.hpp"//完全匹配void boostregex_match(void);//部分数据可以匹配void boostregex_search(void);//替换void boostregex_replace(void);//关于重复和贪婪void Test1(void);//regex_iteratorvoid Test2(void);//regex_token_iterator void Test3(void);int main() { //boostregex_search();//boostregex_replace();//Test1();//Test2();Test3();return 0;}void boostregex_match(void){// 3 digits, a word, any character, 2 digits or "N/A", // a space, then the first word again //用于表示"3个数字, 一个单词, 任意字符, 2个数字或字符串"N/A," 一个空格, 然后重复第一个单词.":boost::regex reg("\\d{3}([a-zA-Z]+).(\\d{2}|N/A)\\s\\1");std::string correct = "123Hello N/A Hello";std::string incorrect = "123Hello 12 hello";assert(boost::regex_match(correct, reg) == true);assert(boost::regex_match(incorrect, reg) == false);return;}//部分数据可以匹配void boostregex_search(void){// "new" and "delete" 出现的次数是否一样? boost::regex reg("(new)|(delete)"); boost::smatch m; std::string s= "Calls to new must be followed by delete. \ Calling simply new results in a leak!"; int new_counter=0; int delete_counter=0; std::string::const_iterator it=s.begin(); std::string::const_iterator end=s.end(); while (boost::regex_search(it,end,m,reg)) { // 是 new 还是 delete? m[1].matched ? ++new_counter : ++delete_counter; it=m[0].second; } if (new_counter!=delete_counter) std::cout << "Leak detected!\n"; else std::cout << "Seems ok...\n";return;/*Leak detected!请按任意键继续. . .*/}//替换void boostregex_replace(void){boost::regex reg("(Colo)(u)(r)", boost::regex::icase | boost::regex::perl); std::string s = "Colour, colours, color, colourize"; s = boost::regex_replace(s, reg, "$1$3"); std::cout << s;return;/*Color, colors, color, colorize请按任意键继续. . .*/}//关于重复和贪婪void Test1(void){boost::regex reg("(.*)(\\d{2})"); boost::cmatch m; const char* text = "Note that I'm 31 years old, not 32."; if (boost::regex_search(text, m, reg)) {if (m[1].matched) std::cout << "(.*) matched: " << m[1].str() << '\n'; if (m[2].matched) std::cout << "Found the age: " << m[2] << '\n'; }return;/*(.*) matched: Note that I'm 31 years old, notFound the age: 32请按任意键继续. . .*/}class regex_callback { int sum_; public: regex_callback() : sum_(0) {} template <typename T> void operator()(const T& what) {sum_ += atoi(what[1].str().c_str()); } int sum() const { return sum_; }};//regex_iteratorvoid Test2(void){boost::regex reg("(\\d+),?"); std::string s = "1,1,2,3,5,8,13,21"; boost::sregex_iterator it(s.begin(), s.end(), reg); boost::sregex_iterator end; regex_callback c; int sum = for_each(it, end, c).sum();return;}//regex_token_iterator void Test3(void){boost::regex reg("/"); std::string s = "Split/Values/Separated/By/Slashes,"; std::vector<std::string> vec; boost::sregex_token_iterator it(s.begin(), s.end(), reg, -1); boost::sregex_token_iterator end; while (it != end) vec.push_back(*it++); assert(vec.size() == std::count(s.begin(), s.end(), '/') + 1); assert(vec[0] == "Split");return;}
0 0
- Regex
- Regex
- Regex
- Regex
- regex
- Regex
- regex
- Regex
- regex
- Regex
- regex
- Regex
- regex
- Regex
- Regex
- Regex
- regex
- Regex
- Java中的加密算法
- OpenSack Austin Nova Design Summit - Scheduler & Cells v2
- 正则表达式中的特殊字符
- 如何确定自己是否适合做程序员?(转载学习)
- 如何在MySQL存储过程中保留注释
- Regex
- java读取文件的方法
- hibernate query.list() 返回的数据类型
- 关于在一个无序数组中的数求最大值和最小值的最小比较次数
- 非阻塞 connect套接字
- 获取上一个url
- HttpURLConnection用法详解
- MongoDB -- 插入时间少8小时
- ExtJS -- grid没有数据时不出现横向滚动条的问题