Format
std::regex_replace
and std::match_results.format
in combination with capture groups enables you to format text. You can use a format string together with a placeholder to insert the value.
Here are both possibilities:
// format.cpp
...
#include
<regex>
...
std
::
string
future
{
"Future"
};
const
std
::
string
unofficial
{
"unofficial, C++0x"
};
const
std
::
string
official
{
"official, C++11"
};
std
::
regex
regValues
{
"(.*),(.*)"
};
std
::
string
standardText
{
"The $1 name of the new C++ standard is $2."
};
std
::
string
textNow
=
std
::
regex_replace
(
unofficial
,
regValues
,
standardText
);
std
::
cout
<<
textNow
<<
std
::
endl
;
// The unofficial name of the new C++ standard is C++0x.
std
::
smatch
smatch
;
if
(
std
::
regex_match
(
official
,
smatch
,
regValues
)){
std
::
cout
<<
smatch
.
str
();
// official,C++11
std
::
string
textFuture
=
smatch
.
format
(
standardText...