Howard Hinnant | a8d7759 | 2010-08-18 00:13:08 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | a8d7759 | 2010-08-18 00:13:08 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <regex> |
| 11 | |
| 12 | // template <class traits, class charT, class ST, class SA> |
| 13 | // basic_string<charT, ST, SA> |
| 14 | // regex_replace(const basic_string<charT, ST, SA>& s, |
| 15 | // const basic_regex<charT, traits>& e, const charT* fmt, |
| 16 | // regex_constants::match_flag_type flags = |
| 17 | // regex_constants::match_default); |
| 18 | |
| 19 | #include <regex> |
| 20 | #include <cassert> |
Howard Hinnant | a8d7759 | 2010-08-18 00:13:08 +0000 | [diff] [blame] | 21 | |
| 22 | int main() |
| 23 | { |
| 24 | { |
| 25 | std::regex phone_numbers("\\d{3}-\\d{4}"); |
| 26 | std::string phone_book("555-1234, 555-2345, 555-3456"); |
| 27 | std::string r = std::regex_replace(phone_book, phone_numbers, |
| 28 | "123-$&"); |
| 29 | assert(r == "123-555-1234, 123-555-2345, 123-555-3456"); |
| 30 | } |
| 31 | { |
| 32 | std::regex phone_numbers("\\d{3}-\\d{4}"); |
| 33 | std::string phone_book("555-1234, 555-2345, 555-3456"); |
| 34 | std::string r = std::regex_replace(phone_book, phone_numbers, |
| 35 | "123-$&", |
| 36 | std::regex_constants::format_sed); |
| 37 | assert(r == "123-$555-1234, 123-$555-2345, 123-$555-3456"); |
| 38 | } |
| 39 | { |
| 40 | std::regex phone_numbers("\\d{3}-\\d{4}"); |
| 41 | std::string phone_book("555-1234, 555-2345, 555-3456"); |
| 42 | std::string r = std::regex_replace(phone_book, phone_numbers, |
| 43 | "123-&", |
| 44 | std::regex_constants::format_sed); |
| 45 | assert(r == "123-555-1234, 123-555-2345, 123-555-3456"); |
| 46 | } |
| 47 | { |
| 48 | std::regex phone_numbers("\\d{3}-\\d{4}"); |
| 49 | std::string phone_book("555-1234, 555-2345, 555-3456"); |
| 50 | std::string r = std::regex_replace(phone_book, phone_numbers, |
| 51 | "123-$&", |
| 52 | std::regex_constants::format_no_copy); |
| 53 | assert(r == "123-555-1234123-555-2345123-555-3456"); |
| 54 | } |
| 55 | { |
| 56 | std::regex phone_numbers("\\d{3}-\\d{4}"); |
| 57 | std::string phone_book("555-1234, 555-2345, 555-3456"); |
| 58 | std::string r = std::regex_replace(phone_book, phone_numbers, |
| 59 | "123-$&", |
| 60 | std::regex_constants::format_first_only); |
| 61 | assert(r == "123-555-1234, 555-2345, 555-3456"); |
| 62 | } |
| 63 | { |
| 64 | std::regex phone_numbers("\\d{3}-\\d{4}"); |
| 65 | std::string phone_book("555-1234, 555-2345, 555-3456"); |
| 66 | std::string r = std::regex_replace(phone_book, phone_numbers, |
| 67 | "123-$&", |
| 68 | std::regex_constants::format_first_only | |
| 69 | std::regex_constants::format_no_copy); |
| 70 | assert(r == "123-555-1234"); |
| 71 | } |
| 72 | } |