lookahead_capture.pass.cpp 2.7 KB

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