用法:
1 提供列正则表达式的匹配,寻找和替换功能
2 匹配用法
boost::regex reg(".");
assert(boost::regex_match(“a”,reg)==true);
3 查找用法
boost::regex reg("(xxx))");
boost::smatch m;
std::string s=
"Calls to xxx. \
Calling xxx!";
if (boost::regex_search(s,m,reg)) {
if (m[1].matched)
std::cout << "The expression (xxx) matched!\n";
}
4 替换
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");
5 遍历
boost::regex reg4("[^13579]");
std::string s="0123456789";
boost::sregex_iterator it(s.begin(),s.end(),reg4);
boost::sregex_iterator end;
while (it!=end)
std::cout << *it++;
6 分割
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");
总结,略