Boost正则表达式
在C++編程中,有一點讓人挺遺憾的就是C++尚不支持正則表達式,這讓很多用戶為了編寫支持正則表達式程序而不得不放棄C++。然而,Boost.Regex庫填補了C++在這方面的空白,它使C++很好的支持各種引擎的正則表達式。
詳細文章鏈接:http://club.topsage.com/thread-2276543-1-1.html
boost regex筆記:http://blog.csdn.net/shiwei0124/article/details/4553665
??????? 在C++編程中,有一點讓人挺遺憾的就是C++尚不支持正則表達式,這讓很多用戶為了編寫支持正則表達式程序而不得不放棄C++。然而,Boost.Regex庫填補了C++在這方面的空白,它使C++很好的支持各種引擎的正則表達式。
?????? 結合我的學習,逐步分析Boost.Regex庫。
?????? ……
?????? Boost.Regex默認使用Perl正則表達式,關于Perl正則表達式的使用,這里就不多說明了,可以參考相關資料。
?????? Boost的正則表達式封裝在boost::basic_regex對象中,與std::basic_string一樣,boost::basic_regex表示的是一族類,也與std::basic_string一樣typedef了幾個特例:
????????????? typedef basic_regex<char> regex;
????????????? typedef basic_regex<wchar>wregex;
?????? Boost.Regex的幾個中重要的成員函數如下:
1.?????? explicit basic_regex(const CharT* p,flag_type regex_constants::normal);
該構造函數接受一個包含正則表達式的字符序列,以及一個表示正則表達式所選的參數信息——例如是否大小寫或使用什么引擎的正則表達式。這里注意,如果傳遞的字符序列是無效的正則表達式,則會拋出異常regex_error。
2.?????? bool emply()const;
該成員函數檢測basic_regex示例是否包含有效的正則表達式。
3.?????? unsigned mark_count() const;
該成員函數返回該basic_regex示例中的正則表達式包含的有標記的子表達式的個數。
4.?????? flag_type flags()const;
該函數返回一個位掩碼,其中包含basic_regex所設置的選項標志。具體標志選項有:icase—忽略字符大小寫和JavaScript—regex使用JavaScript語法
?
另外,Boost.Regex定義了幾個自由函數,實現正則表達式的匹配查詢及修改:
1.?????? 判斷正則表達式時候匹配整個字符串
??
?1template?<typename?CharT,typename?Allocator,typename?traits>?2
?3bool?regex_match(
?4
?5const?CharT*,?
?6
?7match_result<const?chart*,Allocator>&m,
?8
?9const?basic_regex<CharT,traits>&?reg,
10
11match_flag_type?flags=match_default);
12
?
2.?????? 查找字符串中與正則表達式匹配的子序列
??
template?<typename?CharT,typename?Allocator,typename?traits>bool?regex_rearch(
const?CharT*,?
match_result<const?chart*,Allocator>&m,
const?basic_regex<CharT,traits>&?reg,
match_flag_type?flags=match_default);
?
3.?????? 查找字符串中所有正則表達式的匹配,并根據參數fmt格式化所匹配的串
??
template?<typename?CharT,typename?Allocator,typename?traits>basic_string<CharT?>?regex_replace(
const?basic_string<CharT?>&?s,???
const?basic_regex<CharT,traits>&?reg,
const?basic_string<CharT?>&?fmt,???????
match_flag_type?flags=match_default);
?
使用說明
1.?????? 創建regex對象:
??
Include<boost/regex.hpp>regex?reg(“(.*)”);
?
2.?????? regex_match
該函數用來對一個字符串的完全匹配,在很多校驗信息中可以廣泛使用,具體使用示例見附后的測試代碼
3.?????? regex_rearch
說到這個函數,必須要說明下boost.match_result。 regex_rearch在執行查找時,通過一個match_result類型的對象來報告匹配的自表達式。
match_result主要封裝了一個std::vector<sub_match<<…>> >類型的對象,而sub_match類繼承自std::pair,主要記錄匹配的結果信息。關于match_result和sub_match的詳細了解可以閱讀boost設計源碼:
?
????????? 使用示例查看附后的測試源碼。
4.?????? regex_replace
該函數根據指定的fmt格式化通過正則表達式匹配的子串。需要注意的是,該函數不會修改原字符串,只是將格式化后的結果返回。具體使用示例見附后測試源碼。
5.?????? regex_iterator
通過多次調用regex_rearch我們可以處理所有滿足匹配的字串。但是,Regex庫還給我們提供了一個更優雅的方法——即通過regex_iterator。通過字符串和正則表達式構造regex_iterator的時候會構建一個match_result的對象用于保存匹配結果信息,再通過重載++運算符達到遍歷所有匹配信息的目的。關于regex_iterator的詳細了解可以參考Regex的設計源碼:
?????????????
使用示例查看附后的測試源碼。
6.?????? regex_token_iterator
與regex_iterator相似,Regex還提供了一個列舉與正則表達式不匹配的子表達式,就是regex_token_iterator。與stl的設計類似,是通過迭代器適配器實現的。這個特性讓我們很容易的分割字符串。關于regex_token_iterator的詳細了解請查看Regex的設計源碼:
?
????????????? 7.測試源碼
????????????????????
?1#include<iostream>?2#include<string>
?3#include<cassert>
?4#include<boost/regex.hpp>
?5#include<vector>
?6#include<iterator>
?7
?8bool?validate_identify_card(const?std::string?&s){
?9????boost::regex?reg("//d{17}[A-Z0-9]");
10????return?boost::regex_match(s,reg);
11}
12class?regex_callback{
13????public:
14????template<typename?T>
15????void?operator()(const?T&?what){
16????????std::cout<<what[1].str()<<std::endl;
17????}
18};
19
20int?main(){
21????{
22????????boost::regex?reg("//d{3}([a-zA-Z]+).(//d{2}|N/A)//s//1");
23????????std::string?correct="123Hello?N/A?Hello";
24????????std::string?incorrect="123Hello?12?hello";
25????????assert(boost::regex_match(correct,reg)==true);
26????????assert(boost::regex_match(incorrect,reg)==false);
27????}
28????{
29????????std::string?s="421124598608976345";
30????????assert(validate_identify_card(s)==true);
31????}
32????{
33????????boost::regex?reg("(new)|(delete)");
34????????boost::smatch?m;
35????????int?new_counter=0,delete_counter=0;
36????????std::string?s="Calls?to?new?must?be?followed?by?delete.Calling?simply?new?results?in?a?Leak!";
37????????std::string::const_iterator?it=s.begin();
38????????std::string::const_iterator?end=s.end();
39????????while(boost::regex_search(it,end,m,reg)){//這里參數必須是const屬性
40????????????if(m[1].matched){
41????????????????std::cout<<"The?expression(new)?matched"<<std::endl;
42????????????????new_counter++;
43????????????}
44????????????if(m[2].matched){
45????????????????std::cout<<"The?expression(delete)?matched"<<std::endl;
46????????????????delete_counter++;
47????????????}
48????????????it=m[0].second;
49????????}
50????????std::cout<<"new_counter="<<new_counter<<std::endl;
51????????std::cout<<"delete_counter="<<delete_counter<<std::endl;
52????}
53????{//使用boost::regex_replace
54????????boost::regex?reg("(colo)(u)(r)",boost::regex::icase|boost::regex::perl);
55????????std::string?s="Colour,colour,color,colOurize";
56????????s=boost::regex_replace(s,reg,"$1$3");//regex_replace不直接修改s的值,而是返回新值
57????????std::cout<<s<<std::endl;
58????}
59????{//使用boost::regex_iterator迭代器輸出所有匹配項
60????????boost::regex?reg("(//d+),?");
61????????std::string?s="1,2,3,4,5,6,7,85,ad2348(,hj";
62????????boost::sregex_iterator?it(s.begin(),s.end(),reg);
63????????boost::sregex_iterator?end;
64????????for_each(it,end,regex_callback());
65????}
66????{
67????????boost::regex?reg("/");
68????????std::vector<std::string>?vec;
69????????std::string?s="Split/Vulue/Teather/Neusoft/Write/By/Lanwei";
70????????boost::sregex_token_iterator?it(s.begin(),s.end(),reg,-1);
71????????boost::sregex_token_iterator?end;
72????????while(it!=end)
73????????????vec.push_back(*it++);
74????????copy(vec.begin(),vec.end(),std::ostream_iterator<std::string>(std::cout,"/n"));
75????}
76????{
77????????boost::smatch?m;
78????????boost::regex?reg("(new)|(delete)");
79????????std::string?s="Calls?to?new?must?be?followed?by?delete.Calling?simply?new?results?in?a?Leak!";
80????????std::string::const_iterator?it=s.begin();
81????????std::string::const_iterator?end=s.end();
82????????while(boost::regex_search(it,end,m,reg)){
83????????????std::cout<<"size="<<m.size()<<std::endl;
84????????????std::cout<<"m[-2]="<<m[-2]<<std::endl;
85????????????std::cout<<"m[-1]="<<m[-1]<<std::endl;
86????????????std::cout<<"m[0]="<<m[0]<<std::endl;
87????????????std::cout<<"m[1]="<<m[1]<<std::endl;
88????????????//std::cout<<"m[2].type="<<typeid(m[2].first).name()<<std::endl;
89????????????std::cout<<"m[2]="<<m[2]<<std::endl;
90???????????//?std::cout<<"m[2].first="<<m[2].first<<std::endl;
91????????????//std::cout<<"m[2].second="<<m[2].second<<std::endl;
92????????????it=m[0].second;
93????????}
94????}
95????return?0;
96}
97
?
學習小結:
????? ??? 關于Boost.Regex庫的初步學習暫時告以段落。這是個非常有用的庫,打破了用戶只有通過POSIX C的API實現正則表達式的局限。然而,這是一個偉大的庫博大精深的庫,以上的了解只不過的鳳毛麟角,其內部還有很多的隱藏秘密需要花大量的時間去挖掘,探索。
????????? 所以,如果不是純粹的要長期使用C++boost的話,就不要在Boost里面探索了!!!
總結
以上是生活随笔為你收集整理的Boost正则表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 忘记自己家的密码-忘记自己家的密码了
- 下一篇: Vtk读取并显示保存图像