Namensräume
Varianten
Aktionen

std::regex_replace

Von cppreference.com
< cpp‎ | regex
 
 
 
Bibliothek für reguläre Ausdrücke
Klassen
(C++11)
Algorithmen
regex_replace
(C++11)
Iteratoren
Ausnahmen
Traits
Konstanten
(C++11)
Reguläre Ausdrucksgrammatiken
 
Definiert in Header <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) (seit 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) (seit 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) (seit 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) (seit 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) (seit 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) (seit C++11)

regex_replace verwendet den regulären Ausdruck re, um Ersetzungen in der Zielzeichenkette durchzuführen.

1,2) Kopiert Zeichen im Bereich [firstlast) nach out, wobei Sequenzen, die mit re übereinstimmen, durch Zeichen ersetzt werden, die von fmt formatiert werden. Äquivalent zu
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) Der Ausdruck /* replace-expr */ ist m.format(out, fmt, flags).
2) Der Ausdruck /* replace-expr */ ist m.format(out, fmt, fmt + std::char_traits<CharT>::length(fmt), flags).
3,4) Äquivalent zu std::basic_string<CharT, STraits, SAlloc> result;
regex_replace(std::back_inserter(result),
              str.begin(), str.end(), re, fmt, flags);
return result;
.
5,6) Äquivalent zu 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;
.

Inhalt

[edit] Parameter

first, last - der Zielzeichenkettenbereich
str - die Ziel-std::string
s - die nullterminierte C-Style-Zeichenkette
re - der reguläre Ausdruck
fmt - der reguläre Ausdruck für die Ersetzung, die genaue Syntax hängt vom Wert von flags ab
flags - Flags, die bestimmen, wie der Abgleich durchgeführt werden soll
out - Ausgabe-Iterator zum Speichern des Ergebnisses der Ersetzung

[edit] Rückgabewert

Wie oben beschrieben.

[edit] Ausnahmen

std::regex_error kann ausgelöst werden, um eine Fehlerbedingung anzuzeigen.

[edit] Beispiel

#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';
}

Ausgabe

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

[edit] Fehlerberichte

Die folgenden Verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR angewendet auf Verhalten wie veröffentlicht Korrigiertes Verhalten
LWG 2213 C++11 out wurde durch die Ersetzungen nicht aktualisiert out wird aktualisiert

[edit] Siehe auch

versucht, einen regulären Ausdruck an einen beliebigen Teil einer Zeichensequenz anzupassen
(Funktionsvorlage) [bearbeiten]
Optionen speziell für die Übereinstimmung
(Typalias) [bearbeiten]
ersetzt einen angegebenen Teil eines Strings
(public member function of std::basic_string<CharT,Traits,Allocator>) [edit]