test6.pass.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 traits, class charT>
  10. // basic_string<charT>
  11. // regex_replace(const charT* s,
  12. // const basic_regex<charT, traits>& e,
  13. // const charT* fmt,
  14. // regex_constants::match_flag_type flags =
  15. // regex_constants::match_default);
  16. #include <regex>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. int main(int, char**)
  20. {
  21. {
  22. std::regex phone_numbers("\\d{3}-\\d{4}");
  23. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  24. std::string r = std::regex_replace(phone_book, phone_numbers,
  25. "123-$&");
  26. assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
  27. }
  28. {
  29. std::regex phone_numbers("\\d{3}-\\d{4}");
  30. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  31. std::string r = std::regex_replace(phone_book, phone_numbers,
  32. "123-$&",
  33. std::regex_constants::format_sed);
  34. assert(r == "123-$555-1234, 123-$555-2345, 123-$555-3456");
  35. }
  36. {
  37. std::regex phone_numbers("\\d{3}-\\d{4}");
  38. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  39. std::string r = std::regex_replace(phone_book, phone_numbers,
  40. "123-&",
  41. std::regex_constants::format_sed);
  42. assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
  43. }
  44. {
  45. std::regex phone_numbers("\\d{3}-\\d{4}");
  46. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  47. std::string r = std::regex_replace(phone_book, phone_numbers,
  48. "123-$&",
  49. std::regex_constants::format_no_copy);
  50. assert(r == "123-555-1234123-555-2345123-555-3456");
  51. }
  52. {
  53. std::regex phone_numbers("\\d{3}-\\d{4}");
  54. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  55. std::string r = std::regex_replace(phone_book, phone_numbers,
  56. "123-$&",
  57. std::regex_constants::format_first_only);
  58. assert(r == "123-555-1234, 555-2345, 555-3456");
  59. }
  60. {
  61. std::regex phone_numbers("\\d{3}-\\d{4}");
  62. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  63. std::string r = std::regex_replace(phone_book, phone_numbers,
  64. "123-$&",
  65. std::regex_constants::format_first_only |
  66. std::regex_constants::format_no_copy);
  67. assert(r == "123-555-1234");
  68. }
  69. return 0;
  70. }