std::regex_replace
| ヘッダ <regex> で定義
|
||
| template< class OutputIt, class BidirIt, class Traits, class CharT, |
(1) | (C++11以上) |
| template< class OutputIt, class BidirIt, class Traits, class CharT > |
(2) | (C++11以上) |
| template< class Traits, class CharT, class STraits, class SAlloc, |
(3) | (C++11以上) |
| template< class Traits, class CharT, class STraits, class SAlloc > |
(4) | (C++11以上) |
| template< class Traits, class CharT, class STraits, class SAlloc > |
(5) | (C++11以上) |
| template< class Traits, class CharT > std::basic_string<CharT> |
(6) | (C++11以上) |
regex_replace は文字シーケンスの置換を行うために正規表現を使用します。
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にコピーされます。 -
flagsに std::regex_constants::format_no_copy が含まれていれば、マッチしなかった部分シーケンスをoutにコピーしません。 -
flagsに std::regex_constants::format_first_only が含まれていれば、最初のマッチのみが置換されます。
- std::regex_iterator<BidirIt, CharT, traits> i(first, last, re, flags) によって行われたかのように std::regex_iterator オブジェクト
result を構築し、 std::regex_replace(std::back_inserter(result), s.begin(), s.end(), re, fmt, flags) を呼びます。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 | - | 置換の結果を格納する出力イテレータ |
| 型の要件 | ||
-OutputIt は LegacyOutputIterator の要件を満たさなければなりません。
| ||
-BidirIt は LegacyBidirectionalIterator の要件を満たさなければなりません。
| ||
[編集] 戻り値
out のコピーを返します。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
[編集] 関連項目
| (C++11) |
文字シーケンスの任意の部分への正規表現のマッチを試みます (関数テンプレート) |
| (C++11) |
マッチングに固有のオプション (typedef) |