match_results::format

 

Formats submatches.

Syntax

template<class OutIt>
    OutIt format(OutIt out,
        const string_type& fmt, match_flag_type flags = format_default) const;
string_type format(const string_type& fmt, match_flag_type flags = format_default) const;

Parameters

  • OutIt
    The output iterator type.

  • out
    The output stream to write to.

  • fmt
    The format string.

  • flags
    The format flags.

Remarks

Each member function generates formatted text under the control of the format fmt. The first member function writes the formatted text to the sequence defined by its argument out and returns out. The second member function returns a string object holding a copy of the formatted text.

To generate formatted text. literal text in the format string is ordinarily copied to the target sequence. Each escape sequence in the format string is replaced by the text that it represents. The details of the copying and replacement are controlled by the format flags passed to the function.

Example

 

// std__regex__match_results_format.cpp 
// compile with: /EHsc 
#include <regex> 
#include <iostream> 

int main() 
    { 
    std::regex rx("c(a*)|(b)"); 
    std::cmatch mr; 

    std::regex_search("xcaaay", mr, rx); 

    std::cout << "prefix: matched == " << std::boolalpha 
        << mr.prefix().matched 
        << ", value == " << mr.prefix() << std::endl; 
    std::cout << "whole match: " << mr.length() << " chars, value == " 
        << mr.str() << std::endl; 
    std::cout << "suffix: matched == " << std::boolalpha 
        << mr.suffix().matched 
        << ", value == " << mr.suffix() << std::endl; 
    std::cout << std::endl; 

    std::string fmt("\"c(a*)|(b)\" matched \"$0\"\n" 
        "\"(a*)\" matched \"$1\"\n" 
        "\"(b)\" matched \"$2\"\n"); 
    std::cout << mr.format(fmt) << std::endl; 
    std::cout << std::endl; 

// index through submatches 
    for (size_t n = 0; n < mr.size(); ++n) 
        { 
        std::cout << "submatch[" << n << "]: matched == " << std::boolalpha 
            << mr[n].matched << 
            " at position " << mr.position(n) << std::endl; 
        std::cout << "  " << mr.length(n) 
            << " chars, value == " << mr[n] << std::endl; 
        } 
    std::cout << std::endl; 

// iterate through submatches 
    for (std::cmatch::iterator it = mr.begin(); it != mr.end(); ++it) 
        { 
        std::cout << "next submatch: matched == " << std::boolalpha 
            << it->matched << std::endl; 
        std::cout << "  " << it->length() 
            << " chars, value == " << *it << std::endl; 
        } 
    std::cout << std::endl; 

// other members 
    std::cmatch mr1(mr); 
    mr = mr1; 
    mr.swap(mr1); 

    char buf[10]; 
    *mr.format(&buf[0], "<$0>") = '\0'; 
    std::cout << &buf[0] << std::endl; 
    std::cout << "empty == " << std::boolalpha << mr.empty() << std::endl; 

    std::cmatch::allocator_type al = mr.get_allocator(); 
    std::cmatch::string_type str = std::string("x"); 
    std::cmatch::size_type maxsiz = mr.max_size(); 
    std::cmatch::char_type ch = 'x'; 
    std::cmatch::difference_type dif = mr.begin() - mr.end(); 
    std::cmatch::const_iterator cit = mr.begin(); 
    std::cmatch::value_type val = *cit; 
    std::cmatch::const_reference cref = val; 
    std::cmatch::reference ref = val; 

    maxsiz = maxsiz;  // to quiet "unused" warnings 
    if (ref == cref) 
        ch = ch; 
    dif = dif; 

    return (0); 
    } 
prefix: matched == true, value == x
whole match: 4 chars, value == caaa
suffix: matched == true, value == y

"c(a*)|(b)" matched "caaa"
"(a*)" matched "aaa"
"(b)" matched ""


submatch[0]: matched == true at position 1
  4 chars, value == caaa
submatch[1]: matched == true at position 2
  3 chars, value == aaa
submatch[2]: matched == false at position 6
  0 chars, value == 

next submatch: matched == true
  4 chars, value == caaa
next submatch: matched == true
  3 chars, value == aaa
next submatch: matched == false
  0 chars, value == 

<caaa>
empty == false

Requirements

Header: <regex>

Namespace: std

See Also

<regex>
match_results Class
Regular Expressions (C++)