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

#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

	请按任意键继续. . .
	*/
}

你可能感兴趣的:(C++11 正则表达式——实例3)