(四)boost库之正则表达式regex
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                (四)boost库之正则表达式regex
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                正則表達式可以為我們帶來極大的方便,有了它,再也不用為此煩惱
頭文件:
#include <boost/regex.hpp>
1、完全匹配
std::string str("abcd"); boost::regex reg( "a\\w*d" ); if (regex_match(str, reg)) { std::cout << str << " is match" << std::endl; } else { std::cout << str << " is not match" << std::endl; }2、完全匹配并獲取子串
const char* mail = "tengxun@qq.com"; boost::cmatch res; //建立3個子表達式 boost::regex reg("(\\w+)@(\\w+).(\\w+)"); if (boost::regex_match(mail,res, reg)) { //既可以通過迭代器獲取數據, 也可以通過數組方式獲取數據 for (boost::cmatch::iterator pos = res.begin(); pos != res.end(); ++pos) { std::cout << *pos << std::endl; } //res[0]存放匹配到的完整字符串 std::cout << "name:" << res[1] << std::endl; }?
3、查找, 當你不需要匹配整個字符串的時候,可以選擇查找
const char* mail = "tengxun@qq.com.cn"; boost::cmatch res; //建立3個子表達式 boost::regex reg("(\\w+)@(\\w+).(\\w+)"); if (boost::regex_search(mail,res, reg)) { std::cout <<"**************************************" << std::endl; //既可以通過迭代器獲取數據, 也可以通過數組方式獲取數據 for (boost::cmatch::iterator pos = res.begin(); pos != res.end(); ++pos) { std::cout << *pos << std::endl; } //res[0]存放匹配到的完整字符串 std::cout << "match :" << res[0] << std::endl << "name:" << res[1] << std::endl; }4、替換
替換匹配到的子字符串, 可以通過$N 引用第N個匹配到的值、$&? 引用全匹配
#include <boost/algorithm/string.hpp> void TestReplace() { //將tengxun@qq.com.cn 替換成tengxun@139.com.cn std::string mail("tengxun@qq.com.cn"); //建立3個子表達式 boost::regex reg("(\\w+)@(\\w+).(\\w+)"); std::cout << boost::regex_replace(mail, reg, "$1@139.$3") << std::endl; std::cout << boost::regex_replace(mail, reg, "my$1@$2.$3") << std::endl; //自定義替換函數,regex_replace將匹配到的字符串數組傳遞給回調函數,由回調函數返回新的字符串 std::cout << boost::regex_replace(mail, reg, [](const boost::smatch &m){ return boost::to_upper_copy(m[0].str()); }); }5、迭代
當需要從字符串中提取多個表達式時,可以采用迭代進行提取
std::string str("tengxun@qq.com, aa@tt.com, bb@qq.com"); boost::regex reg("(\\w+)@(\\w+).(\\w+)"); boost::sregex_iterator pos(str.begin(), str.end(), reg); boost::sregex_iterator end; while(pos != end) { std::cout << "[" << (*pos)[0] << "]"; ++pos; }?
6、分詞
#include <iostream> #include <boost/regex.hpp> void TestToken() { using namespace std; using namespace boost; string str("tengxun@qq.com, aa@tt.com, bb@qq.com"); regex reg("\\w+"); sregex_token_iterator pos(str.begin(), str.end(), reg); while(pos != sregex_token_iterator()) { cout << "[" << *pos << "]" ; ++pos; } cout << endl; //如果最后一個參數args為-1,則把匹配到的字符串視為分隔符 regex split_reg(","); pos = sregex_token_iterator(str.begin(), str.end(), split_reg, -1); while(pos != sregex_token_iterator()) { cout << "[" << *pos << "]" ; ++pos; } cout << endl; //如果最后一個參數args為正數,則返回匹配結果的第args個子串 regex split_sub_reg("(\\w*)@(\\w*).(\\w*)"); pos = sregex_token_iterator(str.begin(), str.end(), split_sub_reg, 1); while(pos != sregex_token_iterator()) { cout << "[" << *pos << "]" ; ++pos; } cout << endl; //匹配并指定輸出順序 //從下面字符串中提取日期,并轉換成 年月日 的順序輸出 std::string input("01/02/2003 blahblah 04/23/1999 blahblah 11/13/1981"); regex re("(\\d{2})/(\\d{2})/(\\d{4})"); // find a date int const sub_matches[] = { 3, 1, 2 }; // year,month, day sregex_token_iterator begin( input.begin(), input.end(), re, sub_matches ), end; // write all the words to std::cout std::ostream_iterator< std::string > out_iter( std::cout, "\n" ); std::copy( begin, end, out_iter ); }總結
以上是生活随笔為你收集整理的(四)boost库之正则表达式regex的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: (三)Boost库之字符串处理
- 下一篇: (五)boost库之随机数random
