C++11正则表达式例子集合
生活随笔
收集整理的這篇文章主要介紹了
C++11正则表达式例子集合
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <string>
#include <regex>
#include <iostream>
using namespace std;int testRegexSearch()
{//定義正則表達(dá)式,匹配時(shí)間格式regex testRegex("[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}");//要匹配的字符串string strText("OTA LOG SFTCH/MPA Stream 2/Reservation Accept 07:23:50.580 Channel: 147, Pilot PN: 232");//結(jié)果:cmatch只能存儲(chǔ)一個(gè)結(jié)果cmatch result; //search 是匹配子字符串, match 是匹配整個(gè)字符串if (regex_search(strText.c_str(), result, testRegex, regex_constants::format_default)){cout << result.str() << endl;}else{cout << "fail." << endl;}return 0;
}bool test_email_valid(const std::string& email)
{const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");bool isValid = std::regex_match(email, pattern);std::cout << email << " : " << (isValid ? "valid" : "invalid") << std::endl;return isValid;
}int testRegexMatch()
{std::string email1 = "marius.bancila@domain.com"; std::string email2 = "mariusbancila@domain.com"; std::string email3 = "marius_b@domain.co.uk"; std::string email4 = "marius@domain"; test_email_valid(email1);test_email_valid(email2);test_email_valid(email3);test_email_valid(email4); return 0;
}void show_ip_parts(const std::string& ip)
{// IP格式const std::regex pattern("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})"); // object that will contain the sequence of sub-matches std:: match_results<std::string::const_iterator> result;// match the IP address with the regular expression bool valid = std:: regex_match(ip, result, pattern); std::cout << ip << " \t: " << (valid ? "valid" : "invalid") << std::endl;// if the IP address matched the regex, then print the parts if(valid) { std::cout << "b1: " << result[1] << std::endl; std::cout << "b2: " << result[2] << std::endl; std::cout << "b3: " << result[3] << std::endl; std::cout << "b4: " << result[4] << std::endl; }
}int testMatchResult()
{ show_ip_parts("1:22:33:444"); show_ip_parts("1:22:33:4444"); show_ip_parts("100:200"); return 0;
}int testMultiSearch()
{ // 星期模式const std::regex pattern("\\w+day"); // 文本 std::string weekend = "Saturday and Sunday, but some Fridays also.";//需要注意一下這里const std::sregex_token_iterator end;for (std::sregex_token_iterator it(weekend.begin(),weekend.end(), pattern); it != end ; ++it){std::cout << *it << std::endl;}std::cout<<std::endl;return 0;
}int regexReplace()
{ // 需要轉(zhuǎn)換的文本std::string text = "This is a element and this a unique ID."; // regular expression with two capture groupsconst std::regex pattern("(\\ba (a|e|i|u|o))+");// the pattern for the transformation, using the second// capture groupstd::string replace = "an $2";std::string newtext = std::regex_replace(text, pattern, replace);std::cout << newtext << std::endl;std::cout << std::endl;return 0;
}std::string format_date(const std::string& date)
{ // regular expression const std:: regex pattern("(\\d{1,2})(\\.|-|/)(\\d{1,2})(\\.|-|/)(\\d{4})");// transformation pattern, reverses the position of all capture groupsstd::string replacer = "$5$4$3$2$1";// apply the tranformationreturn std:: regex_replace(date, pattern, replacer);
}int regexReplaceDate()
{ std::string date1 = "1/2/2008"; std::string date2 = "12.08.2008"; std::cout << date1 << " -> " << format_date(date1) << std::endl; std::cout << date2 << " -> " << format_date(date2) << std::endl; std::cout << std::endl; return 0;
}int searchCount() {// "new" and "delete" 出現(xiàn)的次數(shù)是否一樣? std::regex reg("(new)|(delete)"); std::smatch m; std::string s = "Calls to new must be followed by delete. Calling simply new results in a leak!"; int new_counter=0; int delete_counter=0; std::string::const_iterator it=s.begin(); std::string::const_iterator end=s.end(); while (std::regex_search(it, end, m, reg)){// 是 new 還是 delete?m[1].matched ? ++new_counter : ++delete_counter;it=m[0].second;}if (new_counter!=delete_counter) {std::cout << "Leak detected!\n";}else {std::cout << "Seems ok...\n";}std::cout << std::endl;return 0;
}int testRegexMain()
{// 1) 搜索時(shí)間格式 07:23:50.580 并顯示出來cout << ">>>>>>>>>>>>>>testRegexSearch" << endl;testRegexSearch();// 2) 檢測email格式是否正確 marius.bancila@domain.comcout << ">>>>>>>>>>>>>>testRegexMatch" << endl;testRegexMatch();// 3) 檢測是否符合IP格式: 1:22:33:4444,如果符合輸出IP的4個(gè)部分內(nèi)容cout << ">>>>>>>>>>>>>>testMatchResult" << endl;testMatchResult();// 4) 查找顯示所有符合星期格式的單詞:Saturdaycout << ">>>>>>>>>>>>>>testMultiSearch" << endl;testMultiSearch();// 5) 替換符合格式的文本內(nèi)容:元音開頭前面的a改成ancout << ">>>>>>>>>>>>>>regexReplace" << endl;regexReplace();// 6) 年月日格式的轉(zhuǎn)換,將DD-MM-YYYY –> YYYY-MM-DDcout << ">>>>>>>>>>>>>>regexReplaceDate" << endl;regexReplaceDate();// 7) "new" and "delete" 出現(xiàn)的次數(shù)是否一樣?cout << ">>>>>>>>>>>>>>searchCount" << endl;searchCount();return 0;
}
總結(jié)
以上是生活随笔為你收集整理的C++11正则表达式例子集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 完成一个休闲网络游戏需要学习的知识
- 下一篇: cocos2d-x2.2九宫格CCSca