最終更新日時(UTC):
が更新

履歴 編集

function template
<regex>

std::match_results::format(C++11)

template <class OutputIter>
OutputIter
format(OutputIter out, const char_type* fmt_first, const char_type* fmt_last,
       regex_constants::match_flag_type flags = regex_constants::format_default) const; // (1)

template <class OutputIter, class ST, class SA>
OutputIter
format(OutputIter out, const basic_string<char_type, ST, SA>& fmt,
       regex_constants::match_flag_type flags = regex_constants::format_default) const; // (2)

template <class ST, class SA>
basic_string<char_type, ST, SA>
format(const basic_string<char_type, ST, SA>& fmt,
       regex_constants::match_flag_type flags = regex_constants::format_default) const; // (3)

string_type
format(const char_type* fmt,
       regex_constants::match_flag_type flags = regex_constants::format_default) const; // (4)

概要

*this を書式文字列に従って出力する。

要件

  • ready() == true であること。
  • テンプレート引数 OutputIter は出力イテレータの要件を満たすこと。

効果

  • (1) 書式文字列 [fmt_first, fmt_last) を出力イテレータ out にコピーする。 コピーされる書式文字列内のエスケープシーケンスや書式指定子は、対応する文字や *this 内の対応する文字列に置換される。
    書式文字列の形式は、ビットマスク flags で指定する。
  • (2) return format(out, fmt.data(), fmt.data() + fmt.size(), flags) と等価。
  • (3) basic_string<char_type, ST, SA> 型の空の文字列変数 result を構築し、format(back_inserter(result), fmt, flags) を呼び出す。
  • (4) string_type 型の空の文字列変数 result を構築し、format(back_inserter(result), fmt, fmt + char_traits<char_type>::length(fmt), flags) を呼び出す。

戻り値

  • (1) out
  • (2) out
  • (3) 構築した文字列変数 result
  • (4) 構築した文字列変数 result

備考

flags に指定可能な(指定して意味のある) regex_constants::match_flag_type のフラグは、以下のいずれかのみである。

  • format_default
  • format_sed

#include <iostream>
#include <regex>

int main()
{
  const char s[] = " 000 abc 123 def 999 ";
  const std::regex re("(\\w+) (\\d+) (\\w+)");
  std::cmatch m;
  if (std::regex_search(s, m, re)) {
    std::cout << m.format("$1$3 [$2]") << std::endl;
  } else {
    std::cout << "not match" << std::endl;
  }
}

出力

abcdef [123]

バージョン

言語

  • C++11

処理系