101 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// <regex>10 11// template <class BidirectionalIterator, class Allocator, class charT, class traits>12// bool13// regex_match(BidirectionalIterator first, BidirectionalIterator last,14// match_results<BidirectionalIterator, Allocator>& m,15// const basic_regex<charT, traits>& e,16// regex_constants::match_flag_type flags = regex_constants::match_default);17 18// std::regex in ECMAScript mode should not ignore capture groups inside lookahead assertions.19// For example, matching /(?=(a))(a)/ to "a" should yield two captures: \1 = "a", \2 = "a"20 21#include <regex>22#include <cassert>23 24#include "test_macros.h"25#include "test_iterators.h"26 27int main(int, char**)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 return 0;100}101