#include <iostream>
#include <regex>
void std_regex() {
std::string str = "C++ Meetup on 2018-09-12 about String Parsing";
std::regex re1("on ([0-9]{4}-[0-9]{2}-[0-9]{2})");
if (std::regex_search(str, re1)) {
std::cout << "std::regex_search() with re1: matched!" << std::endl;
}
else {
std::cout << "std::regex_search() with re1: no match!" << std::endl;
}
if (std::regex_match(str, re1)) {
std::cout << "std::regex_match() with re1: matched!" << std::endl;
}
else {
std::cout << "std::regex_match() with re1: no match!" << std::endl;
}
std::smatch match;
if (std::regex_search(str, match, re1)) {
std::cout << "std::regex_search() with re1: matched!" << std::endl
<< " match.size() = " << match.size() << std::endl
<< " match[0] = " << match[0] << std::endl
<< " match[1] = " << match[1] << std::endl;
}
else {
std::cout << "std::regex_search() with re1: no match!" << std::endl;
}
const char* cstr = "Hello on 2018-09-13";
std::cmatch cmatch;
if (std::regex_search(cstr, cmatch, re1)) {
std::cout << "std::regex_search() with re1: matched!" << std::endl
<< " match.size() = " << cmatch.size() << std::endl
<< " match[0] = " << cmatch[0] << std::endl
<< " match[1] = " << cmatch[1] << std::endl;
}
else {
std::cout << "std::regex_search() with re1: no match!" << std::endl;
}
std::string result = std::regex_replace(str, re1, "TODAY");
std::cout << "std::regex_replace() result = " << result << std::endl;
}
#include <boost/regex.hpp>
void boost_regex() {
std::string str = "C++ Meetup on 2018-09-12 about String Parsing";
boost::regex re1("on ([0-9]{4}-[0-9]{2}-[0-9]{2})");
if (boost::regex_search(str, re1)) {
std::cout << "boost::regex_search() with re1: matched!" << std::endl;
}
else {
std::cout << "boost::regex_search() with re1: no match!" << std::endl;
}
if (boost::regex_match(str, re1)) {
std::cout << "boost::regex_match() with re1: matched!" << std::endl;
}
else {
std::cout << "boost::regex_match() with re1: no match!" << std::endl;
}
boost::smatch match;
if (boost::regex_search(str, match, re1)) {
std::cout << "boost::regex_search() with re1: matched!" << std::endl
<< " match.size() = " << match.size() << std::endl
<< " match[0] = " << match[0] << std::endl
<< " match[1] = " << match[1] << std::endl;
}
else {
std::cout << "boost::regex_search() with re1: no match!" << std::endl;
}
}
#if (__cplusplus >= 201103L)
using std::regex_search;
#else
using boost::regex_search;
#endif
int main()
{
std_regex();
boost_regex();
return 0;
}