This documentation is archived and is not being maintained.

match_results::format

Formats submatches.

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;

OutIt

The output iterator type.

out

The output stream to write to.

fmt

The format string.

flags

The format flags.

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.

 

// std_tr1__regex__match_results_format.cpp 
// compile with: /EHsc 
#include <regex> 
#include <iostream> 
 
int main() 
    { 
    std::tr1::regex rx("c(a*)|(b)"); 
    std::tr1::cmatch mr; 
 
    std::tr1::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::tr1::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::tr1::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::tr1::cmatch::allocator_type al = mr.get_allocator(); 
    std::tr1::cmatch::string_type str = std::string("x"); 
    std::tr1::cmatch::size_type maxsiz = mr.max_size(); 
    std::tr1::cmatch::char_type ch = 'x'; 
    std::tr1::cmatch::difference_type dif = mr.begin() - mr.end(); 
    std::tr1::cmatch::const_iterator cit = mr.begin(); 
    std::tr1::cmatch::value_type val = *cit; 
    std::tr1::cmatch::const_reference cref = val; 
    std::tr1::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

Header: <regex>

Namespace: std::tr1

Show: