std::match_results::format(3)
πΈ NAME πΈ
okay bestie, this is the std::match_results::format(3) page from the C++ Standard Libary manual (typo preserved, we love her). The name is literally std::match_results::format β std::match_results::format. no cap, that's the whole vibe. π
π§ SYNOPSIS π§
All four overloads are (since C++11). Here's the lineup with the exact signatures:
(1)
template< class OutputIt >
OutputIt format( OutputIt out,
const char_type* fmt_first, const char_type* fmt_last,
std::regex_constants::match_flag_type flags =
std::regex_constants::format_default ) const;
(2)
template< class OutputIt, class ST, class SA >
OutputIt format( OutputIt out,
const basic_string<char_type,ST,SA>& fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::format_default ) const;
(3)
template< class ST, class SA >
std::basic_string<char_type,ST,SA>
format( const std::basic_string<char_type,ST,SA>& fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::format_default ) const;
(4)
string_type format( const char_type* fmt_s,
std::regex_constants::match_flag_type flags =
std::regex_constants::format_default ) const;
okay so basically: format outputs a format string, replacing any format specifiers or escape sequences in that string with match data from *this. it's giving mad-libs but make it regex. β¨
- The format character sequence is defined by the range
[fmt_first, fmt_last). The resulting character sequence is copied toout. - The format character sequence is defined by the characters in
fmt. The resulting character sequence is copied toout. - The format character sequence is defined by the characters in
fmtandfmt_srespectively. The resulting character sequence is copied to a newly constructedstd::basic_string, which is returned.
The flags bitmask determines which format specifiers and escape sequences are recognized.
bestie, warning: the behavior offormatis undefined ifready() != true. do not play with that. π
π¦ PARAMETERS π¦
fmt_begin,fmt_endβ pointers to a range of characters defining the format character sequence. (yes, this list saysfmt_begin/fmt_endwhile the synopsis saysfmt_first/fmt_lastβ I'm just the messenger π )fmtβstd::basic_stringdefining the format character sequence.fmt_sβ pointer to a null-terminated character string defining the format character sequence.outβ iterator that the resulting character sequence is copied to.flagsβstd::regex_constants::match_flag_typebitmask specifying which format specifiers and escape sequences are recognized.
π«Ά TYPE REQUIREMENTS π«Ά
OutputItmust meet the requirements ofLegacyOutputIterator.
π RETURN VALUE π
- 1, 2)
out - 3, 4) The newly constructed string containing resulting character sequence.
π EXCEPTIONS π
May throw implementation-defined exceptions.
π EXAMPLE π
// Run this code
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string s = "for a good time, call 867-5309";
std::regex phone_regex("\\d{3}-\\d{4}");
std::smatch phone_match;
if (std::regex_search(s, phone_match, phone_regex))
{
std::string fmt_s = phone_match.format(
"$`" // $` means characters before the match
"[$&]" // $& means the matched characters
"$'"); // $' means characters following the match
std::cout << fmt_s << '\n';
}
}
π OUTPUT π
for a good time, call [867-5309]
π SEE ALSO π
regex_replace(C++11)β replaces occurrences of a regular expression with formatted replacement text (function template).match_flag_type(C++11)β options specific to matching (typedef).
source/version/date sign-off: this page came from http://cppreference.com, version/date 2024.06.10, and its manual page name is std::match_results::format(3). bye bestie, go format something cute ππ¦β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms