Home | History | Annotate | Download | only in re.alg.match
      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_macros.h"
     26 #include "test_iterators.h"
     27 
     28 int main()
     29 {
     30     {
     31         std::regex re("^(?=(.))a$");
     32         assert(re.mark_count() == 1);
     33 
     34         std::string s("a");
     35         std::smatch m;
     36         assert(std::regex_match(s, m, re));
     37         assert(m.size() == 2);
     38         assert(m[0] == "a");
     39         assert(m[1] == "a");
     40     }
     41 
     42     {
     43         std::regex re("^(a)(?=(.))(b)$");
     44         assert(re.mark_count() == 3);
     45 
     46         std::string s("ab");
     47         std::smatch m;
     48         assert(std::regex_match(s, m, re));
     49         assert(m.size() == 4);
     50         assert(m[0] == "ab");
     51         assert(m[1] == "a");
     52         assert(m[2] == "b");
     53         assert(m[3] == "b");
     54     }
     55 
     56     {
     57         std::regex re("^(.)(?=(.)(?=.(.)))(...)$");
     58         assert(re.mark_count() == 4);
     59 
     60         std::string s("abcd");
     61         std::smatch m;
     62         assert(std::regex_match(s, m, re));
     63         assert(m.size() == 5);
     64         assert(m[0] == "abcd");
     65         assert(m[1] == "a");
     66         assert(m[2] == "b");
     67         assert(m[3] == "d");
     68         assert(m[4] == "bcd");
     69     }
     70 
     71     {
     72         std::regex re("^(a)(?!([^b]))(.c)$");
     73         assert(re.mark_count() == 3);
     74 
     75         std::string s("abc");
     76         std::smatch m;
     77         assert(std::regex_match(s, m, re));
     78         assert(m.size() == 4);
     79         assert(m[0] == "abc");
     80         assert(m[1] == "a");
     81         assert(m[2] == "");
     82         assert(m[3] == "bc");
     83     }
     84 
     85     {
     86         std::regex re("^(?!((b)))(?=(.))(?!(abc)).b$");
     87         assert(re.mark_count() == 4);
     88 
     89         std::string s("ab");
     90         std::smatch m;
     91         assert(std::regex_match(s, m, re));
     92         assert(m.size() == 5);
     93         assert(m[0] == "ab");
     94         assert(m[1] == "");
     95         assert(m[2] == "");
     96         assert(m[3] == "a");
     97         assert(m[4] == "");
     98     }
     99 }
    100