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 BidirectionalIterator, class Allocator, class charT, class traits>
13 // bool
14 // regex_match(BidirectionalIterator first, BidirectionalIterator last,
15 // match_results<BidirectionalIterator, Allocator>& m,
16 // const basic_regex<charT, traits>& e,
17 // regex_constants::match_flag_type flags = regex_constants::match_default);
18
19 // std::regex in ECMAScript mode should not ignore capture groups inside lookahead assertions.
20 // For example, matching /(?=(a))(a)/ to "a" should yield two captures: \1 = "a", \2 = "a"
21
22 #include <regex>
23 #include <cassert>
24
25 #include "test_iterators.h"
26
main()27 int main()
28 {
29 {
30 std::regex re("^(?=(.))a$");
31 assert(re.mark_count() == 1);
32
33 std::string s("a");
34 std::smatch m;
35 assert(std::regex_match(s, m, re));
36 assert(m.size() == 2);
37 assert(m[0] == "a");
38 assert(m[1] == "a");
39 }
40
41 {
42 std::regex re("^(a)(?=(.))(b)$");
43 assert(re.mark_count() == 3);
44
45 std::string s("ab");
46 std::smatch m;
47 assert(std::regex_match(s, m, re));
48 assert(m.size() == 4);
49 assert(m[0] == "ab");
50 assert(m[1] == "a");
51 assert(m[2] == "b");
52 assert(m[3] == "b");
53 }
54
55 {
56 std::regex re("^(.)(?=(.)(?=.(.)))(...)$");
57 assert(re.mark_count() == 4);
58
59 std::string s("abcd");
60 std::smatch m;
61 assert(std::regex_match(s, m, re));
62 assert(m.size() == 5);
63 assert(m[0] == "abcd");
64 assert(m[1] == "a");
65 assert(m[2] == "b");
66 assert(m[3] == "d");
67 assert(m[4] == "bcd");
68 }
69
70 {
71 std::regex re("^(a)(?!([^b]))(.c)$");
72 assert(re.mark_count() == 3);
73
74 std::string s("abc");
75 std::smatch m;
76 assert(std::regex_match(s, m, re));
77 assert(m.size() == 4);
78 assert(m[0] == "abc");
79 assert(m[1] == "a");
80 assert(m[2] == "");
81 assert(m[3] == "bc");
82 }
83
84 {
85 std::regex re("^(?!((b)))(?=(.))(?!(abc)).b$");
86 assert(re.mark_count() == 4);
87
88 std::string s("ab");
89 std::smatch m;
90 assert(std::regex_match(s, m, re));
91 assert(m.size() == 5);
92 assert(m[0] == "ab");
93 assert(m[1] == "");
94 assert(m[2] == "");
95 assert(m[3] == "a");
96 assert(m[4] == "");
97 }
98 }
99