test4.pass.cpp 3.1 KB

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