C++:Regex正則表達式

C++:Regex正則表達式



Expression: (ftp|http|https):\/\/((\w+\.)*(\w*))\/([\w\d]+\/{0,1})+ 
    不會用C++的Regex是硬傷,下面的內容參考《C++程序設計原理與實踐》~~~C++:Regex正則表達式C++:Regex正則表達式
****************************************************************************************************
正則表達式語法
     正則表達式的特性分紅不少種類,下面的內容是perl類型的Regex。
============================================================================================
特殊含義的字符
.  :任意單個字符
[] :字符集
{} :計數
() :子模式
\  :下一個字符具備特殊含義
*  :0個或多個
+  :一個或多個
?  :0個或一個
|  :或
^  :行的開始;否認
$  :行的結束
===========================================================================================
字符集
\d :一個十進制數字
\l :一個小寫字母
\s :一個空白符(空格符,製表符等)
\u :一個大寫字母
\w :一個字母(a~z或A~Z)或數字(0~9)或下劃線(_)
\D :除了\d以外的字符
\L :除了\l以外的字符
\S :除了\s以外的字符
\U :除了\u以外的字符
\W :除了\w以外的字符
===========================================================================================
重複
{n}  :嚴格重複n次
{n,} :重複n次或更屢次
{n,m}:重複至少n次,至多m次
*    :{0,}
+    :{1,}
?    :{0,1}
===========================================================================================
子模式
    爲了指定模式中的子模式,用括號將其括起來
(\d*:)?(\d+):它表示字符串前半部分能夠爲空,若非空,則是任意長度的數字後接一個冒號,後半部分是一個或多個數字的序列。
===========================================================================================
可選項
    | 表示二選一的概念。
Subject:(FW:| Re:):表示匹配Subject:Fw:或者是Subject:Re:
===========================================================================================
正則表達式錯誤
    當咱們將一個模式富裕regex時,它會對模式進行檢查,若是發現模式不合法或過於複雜,沒法用於匹配時,它會拋出一個bad_expression異常。
===========================================================================================
下面是一個經常使用的套路,比較穩~~~
C++:Regex正則表達式
注意一點,編譯的時候要指定連接:g++ -Wall -lboost_regex test.cpp -o chen
*****************************************************************************************************
正則表達式方法   
=====================================================================================================
(1)regex_match肯定一行字符串是否和指定的正則表達式徹底匹配
-----------------------------------------------------------------------
//檢查模式是否匹配
  8 #include <boost/regex.hpp>
  9 #include <iostream>
 10 #include <string>
 11
 12 using namespace std;
 13 using namespace boost;
 14
 15 int main()
 16 {
 17     // "\w+\s*(\(\w+,\d+\)\s*)*"
 18     regex pattern("\\w+\\s*(\\(\\w+,\\d+\\)\\s*)*");
 19     cout << pattern << endl;
 20    
 21     string str_1 = "chen (chen,0) (huan,1) (jiang,2)";
 22     string str_2 = "chen(chen,0)(huan,1)(jiang,2)";
 23     string str_3 = "chen";
 24     string str_4 = "(chen,0)(huan,1)(jiang,2)";
 25     string str_5 = "chen (chen,0) (huan,1)(jiang,2) chen";
 26    
 27     vector<string> strings;
 28     strings.push_back(str_1); strings.push_back(str_2);
 29     strings.push_back(str_3); strings.push_back(str_4);
 30     strings.push_back(str_5);
 31    
 32     for(int n = 0 ; n < 5 ; ++n)
 33         if(regex_match(strings[n], pattern))
 34             cout << strings[n] << " is matched" << endl;
 35            
 36     return 0;
 37 }  
結果爲:
\w+\s*(\(\w+,\d+\)\s*)*
chen (chen,0) (huan,1) (jiang,2) is matched
chen(chen,0)(huan,1)(jiang,2) is matched
chen is matched
--------------------------------------------------------------------------
//regex_match不只驗證是否匹配,並且能夠從中提取出正則表達式括號對應的子串
  8 #include <boost/regex.hpp>
  9 #include <iostream>
 10 #include <string>
 11
 12 using namespace std;
 13 using namespace boost;
 14
 15 int main()
 16 {
 17     // "\w+\s*(\(\w+,\d+\)\s*)*"
 18     regex pattern("\\w+\\s*((\\(\\w+,\\d+\\)\\s*)*)");
 19     cout << pattern << endl;
 20    
 21     string str_1 = "chen (chen,0) (huan,1) (jiang,2)";
 22    
 23     smatch mat;
 24     if(regex_match(str_1, mat, pattern))
 25         for(smatch::iterator iter=mat.begin() ; iter!=mat.end() ; ++iter)
 26             cout << *iter <<endl;
 27            
 28     return 0;
 29 }  
結果爲:
\w+\s*((\(\w+,\d+\)\s*)*)
chen (chen,0) (huan,1) (jiang,2)
(chen,0) (huan,1) (jiang,2)
(jiang,2)
注意,這個的regex表達式和上面的不一樣,將後面的子串(\(\w+,\d+\)\s*)*經過括號合併成一個完整的子串。
=====================================================================================================
(2)regex_search:regex_match是驗證是否徹底匹配,而regex_search是從一大串string中找出匹配的一小段字符串
---------------------------------------------------------------
 15 int main()
 16 {
 17     regex pattern("\\d+");                           
 18     cout << pattern << endl;
 19    
 20     string str_1 = "chen1234huan12345jiang1234567";
 21    
 22     smatch mat;
 23     if(regex_search(str_1, mat, pattern))
 24         for(smatch::iterator iter=mat.begin() ; iter!=mat.end() ; ++iter)
 25             cout << *iter <<endl;
 26            
 27     return 0;
 28 }  
結果爲:
1234
能夠看出,regex_search是匹配到字符串中第一個符合條件的模式便會返回。
-------------------------------------------------------------
    下面的方法能夠將字符串中全部匹配到的模式,所有提取出來,以下:
 15 int main()
 16 {
 17     regex pattern("\\d+");                           
 18     cout << pattern << endl;
 19    
 20     string str_1 = "chen1234huan12345jiang1234567";
 21     string::const_iterator start = str_1.begin();
 22     string::const_iterator end = str_1.end();
 23    
 24     smatch mat;
 25     while(regex_search(start, end, mat, pattern))
 26     {  
 27         string msg(mat[0].first, mat[0].second);
 28         cout << msg << endl;
 29         start = mat[0].second;
 30     }  
 31    
 32     return 0;
 33 }  
結果是:
\d+
1234
12345
1234567
****************************************************************************************************
(3)關於regex::smatch類型
    smatch類型,前綴s表示"子匹配"的概念。一個smatch本質上是一個子匹配的向量。第一個元素是完整匹配。若是i < smatch.size(),咱們將smatch[i]當作一個字符串。對於一個正則表達式,若是最後N個子模式,則smatch.size() = N+1(由於有一個完整的匹配)。
    模式中任何放在括號中的內容均可以做爲一個子模式,能夠看下面這個例子:
html

String: http://www.foo.com/bar matches[0] = http://www.foo.com/bar matches[1] = http matches[2] = www.foo.com matches[3] = foo. matches[4] = com matches[5] = bar
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息