名前空間
変種
操作

std::regex_replace

提供: cppreference.com
< cpp‎ | regex
ヘッダ <regex> で定義
template< class OutputIt, class BidirIt,

          class Traits, class CharT,
          class STraits, class SAlloc >
OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last,
                        const std::basic_regex<CharT,Traits>& re,
                        const std::basic_string<CharT,STraits,SAlloc>& fmt,
                        std::regex_constants::match_flag_type flags =

                            std::regex_constants::match_default );
(1) (C++11以上)
template< class OutputIt, class BidirIt,

          class Traits, class CharT >
OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last,
                        const std::basic_regex<CharT,Traits>& re,
                        const CharT* fmt,
                        std::regex_constants::match_flag_type flags =

                            std::regex_constants::match_default );
(2) (C++11以上)
template< class Traits, class CharT,

          class STraits, class SAlloc,
          class FTraits, class FAlloc >
std::basic_string<CharT,STraits,SAlloc>
    regex_replace( const std::basic_string<CharT,STraits,SAlloc>& s,
                   const std::basic_regex<CharT,Traits>& re,
                   const std::basic_string<CharT,FTraits,FAlloc>& fmt,
                   std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default );
(3) (C++11以上)
template< class Traits, class CharT,

          class STraits, class SAlloc >
std::basic_string<CharT,STraits,SAlloc>
    regex_replace( const std::basic_string<CharT,STraits,SAlloc>& s,
                   const std::basic_regex<CharT,Traits>& re,
                   const CharT* fmt,
                   std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default );
(4) (C++11以上)
template< class Traits, class CharT,

          class STraits, class SAlloc >
std::basic_string<CharT>
    regex_replace( const CharT* s,
                   const std::basic_regex<CharT,Traits>& re,
                   const std::basic_string<CharT,STraits,SAlloc>& fmt,
                   std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default );
(5) (C++11以上)
template< class Traits, class CharT >

std::basic_string<CharT>
    regex_replace( const CharT* s,
                   const std::basic_regex<CharT,Traits>& re,
                   const CharT* fmt,
                   std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default );
(6) (C++11以上)

regex_replace は文字シーケンスの置換を行うために正規表現を使用します。

1) re にマッチする任意のシーケンスを fmt でフォーマットした文字に置き換え、範囲 [first,last) 内の文字を out にコピーします。 言い換えると、
  • std::regex_iterator<BidirIt, CharT, traits> i(first, last, re, flags) によって行われたかのように std::regex_iterator オブジェクト i を構築し、シーケンス [first,last) 内の re のすべてのマッチを辿るために使用します。
  • そのようなマッチ m のそれぞれについて、 out = std::copy(m.prefix().first, m.prefix().second, out) によって行われたかのように、マッチしなかった部分シーケンス (m.prefix()) を out にコピーし、 out = m.format(out, fmt, flags) を呼んだかのように、マッチした部分シーケンスをフォーマットされた置換文字列で置き換えます。
  • それ以上マッチが見つからなければ、 out = std::copy(last_m.suffix().first, last_m.suffix().second, out) によって行われたかのように、残りのマッチしなかった文字を out にコピーします。 ただし last_m は最後に見つかったマッチのコピーです。
  • マッチが存在しなければ、 out = std::copy(first, last, out) によって、シーケンス全体がそのまま out にコピーされます。
  • flagsstd::regex_constants::format_no_copy が含まれていれば、マッチしなかった部分シーケンスを out にコピーしません。
  • flagsstd::regex_constants::format_first_only が含まれていれば、最初のマッチのみが置換されます。
2) 1) と同じですが、フォーマットされた置換は out = m.format(out, fmt, fmt + char_traits<charT>::length(fmt), flags) を呼んだかのように行われます。
3-4) std::basic_string<CharT, ST, SA> 型の空文字列 result を構築し、 std::regex_replace(std::back_inserter(result), s.begin(), s.end(), re, fmt, flags) を呼びます。
5-6) std::basic_string<CharT> 型の空文字列 result を構築し、 std::regex_replace(std::back_inserter(result), s, s + std::char_traits<CharT>::length(s), re, fmt, flags) を呼びます。

目次

[編集] 引数

first, last - 一組のイテレータとして表される入力文字シーケンス
s - std::basic_string または文字配列として表される入力文字シーケンス
re - 入力シーケンスに対してマッチされる std::basic_regex
flags - std::regex_constants::match_flag_type 型のマッチフラグ
fmt - 正規表現置換フォーマット文字列。 正確な構文は flags の値に依存します
out - 置換の結果を格���する出力イテレータ
型の要件
-
OutputItLegacyOutputIterator の要件を満たさなければなりません。
-
BidirItLegacyBidirectionalIterator の要件を満たさなければなりません。

[編集] 戻り値

1-2) すべての挿入後の出力イテレータ out のコピーを返します。
3-6) 出力を格納する文字列 result を返します。

[編集] 例外

エラーの状況を表すために std::regex_error を投げる場合があります。

[編集]

#include <iostream>
#include <iterator>
#include <regex>
#include <string>
 
int main()
{
   std::string text = "Quick brown fox";
   std::regex vowel_re("a|e|i|o|u");
 
   // write the results to an output iterator
   std::regex_replace(std::ostreambuf_iterator<char>(std::cout),
                      text.begin(), text.end(), vowel_re, "*");
 
   // construct a string holding the results
   std::cout << '\n' << std::regex_replace(text, vowel_re, "[$&]") << '\n';
}

出力:

Q**ck br*wn f*x
Q[u][i]ck br[o]wn f[o]x

[編集] 関連項目

文字シーケンスの任意の部分への正規表現のマッチを試みます
(関数テンプレート) [edit]
マッチングに固有のオプション
(typedef) [edit]