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