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 // class match_results<BidirectionalIterator, Allocator>
13 
14 // string_type
15 //   format(const char_type* fmt,
16 //          regex_constants::match_flag_type flags = regex_constants::format_default) const;
17 
18 #include <iostream>
19 
20 #include <regex>
21 #include <cassert>
22 
main()23 int main()
24 {
25     {
26         std::match_results<const char*> m;
27         const char s[] = "abcdefghijk";
28         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
29 
30         const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
31         std::string out = m.format(fmt);
32         assert(out == "prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
33     }
34     {
35         std::match_results<const char*> m;
36         const char s[] = "abcdefghijk";
37         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
38 
39         const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
40         std::string out = m.format(fmt, std::regex_constants::format_sed);
41         assert(out == "prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
42     }
43     {
44         std::match_results<const char*> m;
45         const char s[] = "abcdefghijk";
46         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
47 
48         const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
49         std::string out = m.format(fmt, std::regex_constants::format_sed);
50         assert(out == "match: cdefghi, m[1]: efg, m[2]: e");
51     }
52 
53     {
54         std::match_results<const wchar_t*> m;
55         const wchar_t s[] = L"abcdefghijk";
56         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
57 
58         const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
59         std::wstring out = m.format(fmt);
60         assert(out == L"prefix: ab, match: cdefghi, suffix: jk, m[1]: efg, m[2]: e");
61     }
62     {
63         std::match_results<const wchar_t*> m;
64         const wchar_t s[] = L"abcdefghijk";
65         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
66 
67         const wchar_t fmt[] = L"prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
68         std::wstring out = m.format(fmt, std::regex_constants::format_sed);
69         assert(out == L"prefix: $`, match: $cdefghi, suffix: $', m[1]: $1, m[2]: $2");
70     }
71     {
72         std::match_results<const wchar_t*> m;
73         const wchar_t s[] = L"abcdefghijk";
74         assert(std::regex_search(s, m, std::wregex(L"cd((e)fg)hi")));
75 
76         const wchar_t fmt[] = L"match: &, m[1]: \\1, m[2]: \\2";
77         std::wstring out = m.format(fmt, std::regex_constants::format_sed);
78         assert(out == L"match: cdefghi, m[1]: efg, m[2]: e");
79     }
80 }
81