您的位置:首页 > 编程语言 > C语言/C++

C++11 正则表达式——实例3

2016-05-04 00:17 435 查看
#include <regex>
#include <iostream>
#include <string>

//格式化日期
void format_date(void);

int main()
{
format_date();

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);

}

//格式化日期
void format_date(void)
{

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;

/*
1/2/2008 -> 2008/2/1
12.08.2008 -> 2008.08.12

请按任意键继续. . .
*/
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: