regex_search
生活随笔
收集整理的這篇文章主要介紹了
regex_search
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ?在regex_match()里邊只能看到regex和輸入的字符串是不是全部匹配上了,匹配上了就返回true,否則false。然而他不能返回匹配到的子字符串;regex_search()和regex_match()參數類型是一樣的;返回的也是bool類型;但是它還可以查找到匹配的子字符串;將捕捉到的結果會保存在std::smatch里邊;比如:
Std::smatch match;??用過調用match[1] 和 match[2]來查看捕獲到的第一個和第二個捕獲組;
#include <iostream> #include <regex> int main() {std::regex r("//(.+)(\\d{4})");std::string str;while(true){if(!std::getline(std::cin,str) || str == "q"){break;} else{std::smatch match;if(std::regex_search(str,match,r)){std::cout << "find this str : " << match[1] << "***" << match[2]<< std::endl;}else{std::cout << "invaild argument" << std::endl;}std::cout << "prefix() :" << match.prefix() << std::endl;std::cout << "suffix() :" << match.suffix() << std::endl;}}return 0; }輸入:
kslkf//kfskl89438sd
結果:
find this str : kfskl8***9438
prefix() :kslkf
suffix() :sd
其中std::smatch.prefix()返回的是匹配到的捕獲組之前的部分
std::smatch.suffix()返回的是匹配到捕獲組之后的部分;
?
轉載于:https://www.cnblogs.com/boost/p/10425663.html
總結
以上是生活随笔為你收集整理的regex_search的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 进阶之路 (十) 再立Fl
- 下一篇: CF986C AND Graph