【正则表达式1】C++11正则表达式
生活随笔
收集整理的這篇文章主要介紹了
【正则表达式1】C++11正则表达式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
頭文件
#include <regex>regex_match:整個字符串是否匹配
按 Ctrl+C 復制代碼 按 Ctrl+C 復制代碼regex_match:只返回第一個匹配結(jié)果
smatch rr1;smatch rr2;cout << boolalpha << regex_search(s1, rr1, reg1) << endl; //truecout << "s1匹配結(jié)果:" << rr1.str() << endl; //saturdaycout << boolalpha << regex_search(s2, rr2, reg1) << endl; //truecout << "s1匹配結(jié)果:" << rr2.str() << endl; //saturdaycout << endl;iterator:返回多個匹配結(jié)果
類似于指針,調(diào)用成員要用"->"
cout << "iterator結(jié)果:" << endl;sregex_iterator it(s2.begin(), s2.end(), reg1);sregex_iterator end;for(; it != end; ++it){cout << it->str() << endl;//cout << *it << endl; 錯誤 }cout << "token_iterator結(jié)果:" << endl;sregex_token_iterator tit(s2.begin(), s2.end(), reg1);sregex_token_iterator tend;for(; tit != tend; ++tit){cout << tit->str() << endl;cout << *tit << endl;}?
子表達式匹配
regex reg2("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");string ip = "0:11:222:333";smatch m; regex_match(ip, m, reg2);cout << "輸出:str()" << endl;cout << m.str() << endl; //0:11:222:333cout << m.str(1) << endl; //0cout << m.str(2) << endl; //11cout << m.str(3) << endl; //222cout << m.str(4) << endl; //333cout << "輸出:[i]" << endl; //結(jié)果同上cout << m[0] << endl;cout << m[1] << endl;cout << m[2] << endl;cout << m[3] << endl;cout << m[4] << endl;多個匹配結(jié)果
string ip2 = "0:11:222:333 4:55:66:7";sregex_iterator ip_it(ip2.begin(), ip2.end(), reg2);sregex_iterator ip_end;for(; ip_it != ip_end; ++ip_it){cout << ip_it->str() << endl;cout << ip_it->str(1) << endl;cout << ip_it->str(2) << endl;cout << ip_it->str(3) << endl;cout << ip_it->str(4) << endl;}?
總的程序:
#include <iostream> #include <string> #include <regex> using namespace std;int main() {//regex_match匹配整個字符串regex reg1("\\w+day");string s1 = "saturday";string s2 = "saturday and sunday";smatch r1;smatch r2;cout << boolalpha << regex_match(s1, r1, reg1) << endl;cout << boolalpha << regex_match(s2, r2, reg1) << endl;cout << "s1匹配結(jié)果:" << r1.str() << endl;cout << "s2匹配結(jié)果:" << r2.str() << endl;cout << endl;//regex_match只返回第一個匹配結(jié)果 smatch rr1;smatch rr2;cout << boolalpha << regex_search(s1, rr1, reg1) << endl;cout << "s1匹配結(jié)果:" << rr1.str() << endl;cout << boolalpha << regex_search(s2, rr2, reg1) << endl;cout << "s1匹配結(jié)果:" << rr2.str() << endl;cout << endl;//使用iterator返回多個匹配結(jié)果//結(jié)果要用->cout << "iterator結(jié)果:" << endl;sregex_iterator it(s2.begin(), s2.end(), reg1);sregex_iterator end;for(; it != end; ++it){cout << it->str() << endl;//cout << *it << endl; 錯誤 }cout << "token_iterator結(jié)果:" << endl;sregex_token_iterator tit(s2.begin(), s2.end(), reg1);sregex_token_iterator tend;for(; tit != tend; ++tit){cout << tit->str() << endl;cout << *tit << endl;}//子表達式匹配regex reg2("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");string ip = "0:11:222:333";smatch m; regex_match(ip, m, reg2);cout << "輸出:str()" << endl;cout << m.str() << endl;cout << m.str(1) << endl;cout << m.str(2) << endl;cout << m.str(3) << endl;cout << m.str(4) << endl;cout << "輸出:[i]" << endl;cout << m[0] << endl;cout << m[1] << endl;cout << m[2] << endl;cout << m[3] << endl;cout << m[4] << endl;//輸出結(jié)果同上//regex_search(ip, m, str2);cout << endl;string ip2 = "0:11:222:333 4:55:66:7";sregex_iterator ip_it(ip2.begin(), ip2.end(), reg2);sregex_iterator ip_end;for(; ip_it != ip_end; ++ip_it){cout << ip_it->str() << endl;cout << ip_it->str(1) << endl;cout << ip_it->str(2) << endl;cout << ip_it->str(3) << endl;cout << ip_it->str(4) << endl;}return 0;}?
參考:
http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438191.html
http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438215.html
總結(jié)
以上是生活随笔為你收集整理的【正则表达式1】C++11正则表达式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 读懂正则表达式就这么简单
- 下一篇: 正则表达式grep, egrep, fg