欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
C++11 正則表達式——實(shí)例2

 

下面來(lái)介紹和regex_match()很像的regex_search()的使用實(shí)例,regex_match()要求正則表達式必須與模式串完全匹配,regex_search()只要求存在匹配項就可以。

#include <regex>

#include <iostream>

#include <string>

 

int main()

{

  

   const std::tr1::regex pattern("(\\w+day)");

  

   // the source text

   std::string weekend = "Saturday and Sunday";

   std::smatch result;

   bool match = std::regex_search(weekend, result, pattern);

   if(match)

   {

     

      for(size_t i = 1; i < result.size(); ++i)

      {

         std::cout << result[i] << std::endl;

      }

   }

   std::cout<<std::endl;

   return 0;

}

運行結果:

上面這個(gè)例子只能返回第一個(gè)匹配的項,如果要返回所有匹配的子序列,可以使用下面的方式:

#include <regex>

#include <iostream>

#include <string>

 

int main()

{

   // regular expression

   const std::regex pattern("\\w+day");

 

   // the source text

   std::string weekend = "Saturday and Sunday, but some Fridays also.";

 

   const std::sregex_token_iterator end;  //需要注意一下這里

   for (std::sregex_token_iterator i(weekend.begin(),weekend.end(), pattern); i != end ; ++i)

   {

      std::cout << *i << std::endl;

   }

   std::cout<<std::endl;

   return 0;

}

運行結果:

下面的例子將元音字母打頭的單詞前面的a替換為an:

#include <regex>

#include <iostream>

#include <string>

 

int main()

{

   // text to transform

   std::string text = "This is a element and this a unique ID.";

  

   // regular expression with two capture groups

   const std::regex pattern("(\\ba (a|e|i|u|o))+");

  

   // the pattern for the transformation, using the second

   // capture group

   std::string replace = "an $2";

 

   std::string newtext = std::regex_replace(text, pattern, replace);

 

   std::cout << newtext << std::endl;

   std::cout << std::endl;

   return 0;

}

運行結果:

還是來(lái)說(shuō)明一下,這里主要使用了regex_replace(text, pattern, replace),意思是將text的內容按照pattern進(jìn)行匹配,匹配成功的使用replace串進(jìn)行替換,并將替換后的結果作為函數值返回。需要注意的是std::string replace = "an $2"; 這里‘$2’表示模式串的第二個(gè)子表達式,

也就是以a,e,i,o,u開(kāi)頭的單詞。

 

 

 

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
C++11 正則表達式——實(shí)例1,2,3
正則表達式內存耗盡異常解決方案 - 星星的日志 - 網(wǎng)易博客
解析字符串
c 11新特性,所有知識點(diǎn)都在這了!
[轉載]C++:Regex正則表達式
boost安裝總結
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久