Namespaces
Variants

std:: regex_replace

From cppreference.net
(注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造をそのまま保持しています)
定義済みヘッダー <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 > & str,
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 > & str,
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 は正規表現 re を使用して対象文字シーケンスの置換を実行します:

1,2) 範囲 [ first , last ) 内の文字を out にコピーし、 re に一致するシーケンスを fmt でフォーマットされた文字で置換します。以下と同等です:
using iter_type = std::regex_iterator<BidirIt, CharT, Traits>;
iter_type seq_begin(first, last, re, flags), seq_end;
using result_type = std::match_results<BidirIt>;
result_type m;
bool need_to_copy = (flags & std::regex_constants::format_no_copy) == 0;
bool format_all = (flags & std::regex_constants::format_first_only) != 0;
for (iter_type i = seq_begin; i != seq.end(); ++i)
{
    m = *i;
    if (need_to_copy)
        out = std::copy(m.prefix().first, m.prefix().second, out);
    if (format_all || i == seq_begin)
        out = /* replace-expr */
}
if (need_to_copy)
    out = m.ready()
              ? std::copy(m.suffix().first, m.suffix().second, out)
              : std::copy(first, last, out);
return out;
1) /* replace-expr */ m. format ( out, fmt, flags ) です。
2) /* replace-expr */ m. format ( out, fmt, fmt + std:: char_traits < CharT > :: length ( fmt ) , flags ) です。
3,4) 次と同等: std:: basic_string < CharT, STraits, SAlloc > result ;
regex_replace ( std:: back_inserter ( result ) ,
str. begin ( ) , str. end ( ) , re, fmt, flags ) ;
return result ;
.
5,6) 次と同等: std:: basic_string < CharT, STraits, SAlloc > result ;
regex_replace ( std:: back_inserter ( result ) ,
s, s + std:: char_traits < CharT > :: length ( s ) , re, fmt, flags ) ;
return result ;
.

目次

パラメータ

first, last - 対象文字範囲
str - 対象の std::string
s - 対象のnull終端Cスタイル文字列
re - 正規表現
fmt - 正規表現置換フォーマット文字列、正確な構文は flags の値に依存
flags - マッチング方法を決定するフラグ
out - 置換結果を格納する出力イテレータ

戻り値

上記の通り。

例外

エラー状態を示すために std::regex_error をスローする可能性があります error condition

#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");
    // 結果を出力イテレータに書き込む
    std::regex_replace(std::ostreambuf_iterator<char>(std::cout),
                       text.begin(), text.end(), vowel_re, "*");
    // 結果を保持する文字列を構築
    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

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2213 C++11 out 置換によって更新されなかった out 更新される

関連項目

正規表現を文字シーケンスの任意の部分にマッチさせる試み
(関数テンプレート)
マッチングに固有のオプション
(typedef)
文字列の指定された部分を置換する
( std::basic_string<CharT,Traits,Allocator> の公開メンバ関数)