blob: a00ac75193d5438cb75c47d5ad4357b6c799795e [file] [log] [blame]
Howard Hinnant86550b02010-08-18 00:13:08 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant86550b02010-08-18 00:13:08 +00007//
8//===----------------------------------------------------------------------===//
9
10// <regex>
11
12// template <class traits, class charT>
13// basic_string<charT>
14// regex_replace(const charT* s,
15// const basic_regex<charT, traits>& e,
16// const charT* fmt,
17// regex_constants::match_flag_type flags =
18// regex_constants::match_default);
19
20#include <regex>
21#include <cassert>
Marshall Clowfd5ceb22016-04-26 16:24:44 +000022#include "test_macros.h"
Howard Hinnant86550b02010-08-18 00:13:08 +000023
24int main()
25{
26 {
27 std::regex phone_numbers("\\d{3}-\\d{4}");
28 const char phone_book[] = "555-1234, 555-2345, 555-3456";
29 std::string r = std::regex_replace(phone_book, phone_numbers,
30 "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 const char phone_book[] = "555-1234, 555-2345, 555-3456";
36 std::string r = std::regex_replace(phone_book, phone_numbers,
37 "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 const char phone_book[] = "555-1234, 555-2345, 555-3456";
44 std::string r = std::regex_replace(phone_book, phone_numbers,
45 "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 const char phone_book[] = "555-1234, 555-2345, 555-3456";
52 std::string r = std::regex_replace(phone_book, phone_numbers,
53 "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 const char phone_book[] = "555-1234, 555-2345, 555-3456";
60 std::string r = std::regex_replace(phone_book, phone_numbers,
61 "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 const char phone_book[] = "555-1234, 555-2345, 555-3456";
68 std::string r = std::regex_replace(phone_book, phone_numbers,
69 "123-$&",
70 std::regex_constants::format_first_only |
71 std::regex_constants::format_no_copy);
72 assert(r == "123-555-1234");
73 }
74}