#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_iterator
void 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, not
Found 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_iterator
void 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;
}
Regex
最新推荐文章于 2021-07-27 19:07:41 发布