xpressive是一个先进的,灵活的,功能强大的正则表达式库,提供了对正则表达式的全面支持,而且比原正则表达式库boost.regex要好的是它不需要编译,速度快,同时语法又很类似。
以下是对匹配,查找,替换的基本用法
#include <boost/xpressive/xpressive_dynamic.hpp>
#include <boost/typeof/typeof.hpp>
#include <iostream>
#include <string>
void Cregex_use()
{
using namespace boost::xpressive;
cregex reg = cregex::compile("a.c");
assert(regex_match("abc", reg));
assert(regex_match("a+c", reg));
assert(!regex_match("ac", reg));
assert(!regex_match("abdc", reg));
}
void IdCodeMatch()
{
//身份证号的匹配
//地区编码 \d{6}
//出生日 (1|2)\d{3}(0|1)\d[0-3]\d
//最后四位 \d{3}(X|\d)
using namespace boost::xpressive;
// \d{6}(1|2)\d{3}(0|1)\d[0-3]\d\d{3}(X|\d)
cregex reg = cregex::compile("\\d{6}(1|2)\\d{3}(0|1)\\d[0-3]\\d\\d{3}(X|\\d)", icase);//忽略大小写
assert(regex_match("420112199003011234", reg));
assert(regex_match("42011219900301123x", reg));
assert(regex_match("42011229900301123X", reg));
assert(!regex_match("420112399003011234", reg));
assert(!regex_match("420112199023011234", reg));
assert(!regex_match("42011219900301123z", reg));
}
void GetBirthDayFromIdCode()
{
using namespace boost::xpressive;
// \d{6}((1|2)\d{3})((0|1)\d)([0-3]\d)\d{3}(X|\d)
cregex reg = cregex::compile("\\d{6}((1|2)\\d{3})((0|1)\\d)([0-3]\\d)\\d{3}(X|\\d)", icase);
cmatch what;
assert(regex_match("420112199003011234", what,reg));
for (BOOST_AUTO(pos, what.begin()); pos != what.end(); ++pos)
{
std::cout << "[" << * pos << "] ";
}
std::cout << std::endl;
std::cout << "date:" << what[1] << what[3] << what[5] << std::endl;
}
void Regex_search_use()
{
using namespace boost::xpressive;
char * str = "there is POWER-suit item";
// (power)-(.{4})
cregex reg = cregex::compile("(power)-(.{4})", icase);
assert(regex_search(str, reg));
cmatch what;
regex_search(str,what, reg);
assert(what.size() == 3);
std::cout << what[1] << what[2];
assert(!regex_search("error message", reg));
}
void Regex_replace_use()
{
using namespace boost::xpressive;
std::string str("readme.txt");
sregex reg1 = sregex::compile("(.*)(me)");
sregex reg2 = sregex::compile("(t)(.)(t)");
std::cout << regex_replace(str, reg1,"manual") << std::endl;
std::cout << regex_replace(str, reg1,"$1you") << std::endl;
std::cout << regex_replace(str, reg1,"$&$&") << std::endl;
std::cout << regex_replace(str, reg1,"$$") << std::endl;
std::cout << regex_replace(str, reg2,"$1N$3") << std::endl;
std::cout << regex_replace(str, reg2,"$1$3") << std::endl;
}
void Regex_iterator_use()
{
using namespace boost::xpressive;
std::string str("ability,about,abroad,absent are all start with ab");
sregex reg = sregex::compile("ab\\w+");
sregex_iterator pos(str.begin(), str.end(), reg);
sregex_iterator end;
while (pos != end)
{
std::cout << "[" << (*pos)[0]<<"] ";
pos++;
}
std::cout <<" all start with ab"<< std::endl;
}
void Regex_token_iterator_use()
{
using namespace boost::xpressive;
char * str = "*Lilei*||+Mack+||Jack!!||+Lucy";
// 查找出所有的单词
cregex reg = cregex::compile("\\w+", icase);
cregex_token_iterator pos(str, str + strlen(str), reg);
while (pos != cregex_token_iterator())
{
std::cout << "[" << *pos << "] ";
pos++;
}
std::cout << std::endl;
//使用分格符
cregex spilt_reg = cregex::compile("\\|\\|", icase);
// 最后一个参数,如果是-1,则前面的是分格符,如果为正数,则返匹配结果的第args个字符串
pos = cregex_token_iterator(str, str + strlen(str),spilt_reg,-1);
while (pos != cregex_token_iterator())
{
std::cout << "[" << *pos << "] ";
pos++;
}
std::cout << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
//Cregex_use();
//IdCodeMatch();
//GetBirthDayFromIdCode();
//Regex_search_use();
Regex_replace_use();
return 0;
}
结果:
使用的是1.57版本的boost
BOOST_AUTO声明在typeof.hpp中
regex_replace遵循ECMA-262标准的替换。
正则表达式参考:http://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F
更详细的使用参考:http://sns.hwcrazy.com/boost_1_41_0/doc/html/xpressive/user_s_guide.html