• 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, class FST, class FSA>>
13 //     basic_string<charT, ST, SA>
14 //     regex_replace(const basic_string<charT, ST, SA>& s,
15 //                   const basic_regex<charT, traits>& e,
16 //                   const basic_string<charT, FST, FSA>& fmt,
17 //                   regex_constants::match_flag_type flags =
18 //                                              regex_constants::match_default);
19 
20 #include <regex>
21 #include <cassert>
22 #include "test_macros.h"
23 
main()24 int main()
25 {
26     {
27         std::regex phone_numbers("\\d{3}-\\d{4}");
28         std::string phone_book("555-1234, 555-2345, 555-3456");
29         std::string r = std::regex_replace(phone_book, phone_numbers,
30                                            std::string("123-$&"));
31         assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
32     }
33     {
34         std::regex phone_numbers("\\d{3}-\\d{4}");
35         std::string phone_book("555-1234, 555-2345, 555-3456");
36         std::string r = std::regex_replace(phone_book, phone_numbers,
37                                            std::string("123-$&"),
38                                            std::regex_constants::format_sed);
39         assert(r == "123-$555-1234, 123-$555-2345, 123-$555-3456");
40     }
41     {
42         std::regex phone_numbers("\\d{3}-\\d{4}");
43         std::string phone_book("555-1234, 555-2345, 555-3456");
44         std::string r = std::regex_replace(phone_book, phone_numbers,
45                                            std::string("123-&"),
46                                            std::regex_constants::format_sed);
47         assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
48     }
49     {
50         std::regex phone_numbers("\\d{3}-\\d{4}");
51         std::string phone_book("555-1234, 555-2345, 555-3456");
52         std::string r = std::regex_replace(phone_book, phone_numbers,
53                                            std::string("123-$&"),
54                                            std::regex_constants::format_no_copy);
55         assert(r == "123-555-1234123-555-2345123-555-3456");
56     }
57     {
58         std::regex phone_numbers("\\d{3}-\\d{4}");
59         std::string phone_book("555-1234, 555-2345, 555-3456");
60         std::string r = std::regex_replace(phone_book, phone_numbers,
61                                            std::string("123-$&"),
62                                            std::regex_constants::format_first_only);
63         assert(r == "123-555-1234, 555-2345, 555-3456");
64     }
65     {
66         std::regex phone_numbers("\\d{3}-\\d{4}");
67         std::string phone_book("555-1234, 555-2345, 555-3456");
68         std::string r = std::regex_replace(phone_book, phone_numbers,
69                                            std::string("123-$&"),
70                                            std::regex_constants::format_first_only |
71                                            std::regex_constants::format_no_copy);
72         assert(r == "123-555-1234");
73     }
74 }
75