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