Home | History | Annotate | Download | only in re.alg.replace
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <regex>
     11 
     12 // template <class traits, class charT, class ST, class SA>
     13 //     basic_string<charT>
     14 //     regex_replace(const charT* s,
     15 //                   const basic_regex<charT, traits>& e,
     16 //                   const basic_string<charT, ST, SA>& fmt,
     17 //                   regex_constants::match_flag_type flags =
     18 //                                              regex_constants::match_default);
     19 
     20 #include <regex>
     21 #include <cassert>
     22 
     23 int main()
     24 {
     25     {
     26         std::regex phone_numbers("\\d{3}-\\d{4}");
     27         const char phone_book[] = "555-1234, 555-2345, 555-3456";
     28         std::string r = std::regex_replace(phone_book, phone_numbers,
     29                                            std::string("123-$&"));
     30         assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
     31     }
     32     {
     33         std::regex phone_numbers("\\d{3}-\\d{4}");
     34         const char phone_book[] = "555-1234, 555-2345, 555-3456";
     35         std::string r = std::regex_replace(phone_book, phone_numbers,
     36                                            std::string("123-$&"),
     37                                            std::regex_constants::format_sed);
     38         assert(r == "123-$555-1234, 123-$555-2345, 123-$555-3456");
     39     }
     40     {
     41         std::regex phone_numbers("\\d{3}-\\d{4}");
     42         const char phone_book[] = "555-1234, 555-2345, 555-3456";
     43         std::string r = std::regex_replace(phone_book, phone_numbers,
     44                                            std::string("123-&"),
     45                                            std::regex_constants::format_sed);
     46         assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
     47     }
     48     {
     49         std::regex phone_numbers("\\d{3}-\\d{4}");
     50         const char phone_book[] = "555-1234, 555-2345, 555-3456";
     51         std::string r = std::regex_replace(phone_book, phone_numbers,
     52                                            std::string("123-$&"),
     53                                            std::regex_constants::format_no_copy);
     54         assert(r == "123-555-1234123-555-2345123-555-3456");
     55     }
     56     {
     57         std::regex phone_numbers("\\d{3}-\\d{4}");
     58         const char phone_book[] = "555-1234, 555-2345, 555-3456";
     59         std::string r = std::regex_replace(phone_book, phone_numbers,
     60                                            std::string("123-$&"),
     61                                            std::regex_constants::format_first_only);
     62         assert(r == "123-555-1234, 555-2345, 555-3456");
     63     }
     64     {
     65         std::regex phone_numbers("\\d{3}-\\d{4}");
     66         const char phone_book[] = "555-1234, 555-2345, 555-3456";
     67         std::string r = std::regex_replace(phone_book, phone_numbers,
     68                                            std::string("123-$&"),
     69                                            std::regex_constants::format_first_only |
     70                                            std::regex_constants::format_no_copy);
     71         assert(r == "123-555-1234");
     72     }
     73 }
     74