• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, ST, SA>
14 //     regex_replace(const basic_string<charT, ST, SA>& s,
15 //                   const basic_regex<charT, traits>& e, const charT* fmt,
16 //                   regex_constants::match_flag_type flags =
17 //                                              regex_constants::match_default);
18 
19 #include <regex>
20 #include <cassert>
21 #include "test_macros.h"
22 
main()23 int main()
24 {
25     {
26         std::regex phone_numbers("\\d{3}-\\d{4}");
27         std::string phone_book("555-1234, 555-2345, 555-3456");
28         std::string r = std::regex_replace(phone_book, phone_numbers,
29                                            "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         std::string phone_book("555-1234, 555-2345, 555-3456");
35         std::string r = std::regex_replace(phone_book, phone_numbers,
36                                            "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         std::string phone_book("555-1234, 555-2345, 555-3456");
43         std::string r = std::regex_replace(phone_book, phone_numbers,
44                                            "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         std::string phone_book("555-1234, 555-2345, 555-3456");
51         std::string r = std::regex_replace(phone_book, phone_numbers,
52                                            "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         std::string phone_book("555-1234, 555-2345, 555-3456");
59         std::string r = std::regex_replace(phone_book, phone_numbers,
60                                            "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         std::string phone_book("555-1234, 555-2345, 555-3456");
67         std::string r = std::regex_replace(phone_book, phone_numbers,
68                                            "123-$&",
69                                            std::regex_constants::format_first_only |
70                                            std::regex_constants::format_no_copy);
71         assert(r == "123-555-1234");
72     }
73 }
74