2012-07-04 16 views
9

mam te zmienne:Jak użyć metody Zamień wyrażenie regularne zastąpić?

boost::regex re //regular expression to use 
std::string stringToChange //replace this string 
std::string newValue //new value that is going to replace the stringToChange depending on the regex. 

Chcę tylko, aby zastąpić pierwsze wystąpienie tylko od niego.

Dzięki, chłopaki.

EDIT: Znalazłem to:

boost::regex_replace(stringToChange, re, boost::format_first_only); 

ale mówi, że funkcja nie istnieje, zgaduję parametry są nieprawidłowe w tej chwili.

+0

To nie jest poprawna funkcja. –

Odpowiedz

29

Oto przykład podstawowego użycia:

#include <iostream> 
#include <string> 
#include <boost/regex.hpp> 

int main(){ 
    std::string str = "hellooooooooo"; 
    std::string newtext = "o Bob"; 
    boost::regex re("ooooooooo"); 
    std::cout << str << std::endl; 

    std::string result = boost::regex_replace(str, re, newtext); 
    std::cout << result << std::endl; 
} 

Wyjście

hellooooooooo

cześć Bob

Upewnij się, że w tym <boost/regex.hpp> i połączone z biblioteką boost_regex.

Powiązane problemy