#include <string>
#include <regex>
#include <iostream>
using namespace std;
int testRegexSearch()
{
//定义正则表达式,匹配时间格式
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");
//结果:cmatch只能存储一个结果
cmatch result;
//search 是匹配子字符串, match 是匹配整个字符串
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()
{
// 需要转换的文本
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;
}
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 groups
std::string replacer = "$5$4$3$2$1";
// apply the tranformation
return 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" 出现的次数是否一样?
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) 搜索时间格式 07:23:50.580 并显示出来
cout << ">>>>>>>>>>>>>>testRegexSearch" << endl;
testRegexSearch();
// 2) 检测email格式是否正确 marius.bancila@domain.com
cout << ">>>>>>>>>>>>>>testRegexMatch" << endl;
testRegexMatch();
// 3) 检测是否符合IP格式: 1:22:33:4444,如果符合输出IP的4个部分内容
cout << ">>>>>>>>>>>>>>testMatchResult" << endl;
testMatchResult();
// 4) 查找显示所有符合星期格式的单词:Saturday
cout << ">>>>>>>>>>>>>>testMultiSearch" << endl;
testMultiSearch();
// 5) 替换符合格式的文本内容:元音开头前面的a改成an
cout << ">>>>>>>>>>>>>>regexReplace" << endl;
regexReplace();
// 6) 年月日格式的转换,将DD-MM-YYYY –> YYYY-MM-DD
cout << ">>>>>>>>>>>>>>regexReplaceDate" << endl;
regexReplaceDate();
// 7) "new" and "delete" 出现的次数是否一样?
cout << ">>>>>>>>>>>>>>searchCount" << endl;
searchCount();
return 0;
}
C++11正则表达式例子集合
最新推荐文章于 2023-03-07 08:36:29 发布