15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier
95a83710eSEric Fiselier // <regex>
105a83710eSEric Fiselier
115a83710eSEric Fiselier // template <class traits, class charT, class ST, class SA>
125a83710eSEric Fiselier // basic_string<charT>
135a83710eSEric Fiselier // regex_replace(const charT* s,
145a83710eSEric Fiselier // const basic_regex<charT, traits>& e,
155a83710eSEric Fiselier // const basic_string<charT, ST, SA>& fmt,
165a83710eSEric Fiselier // regex_constants::match_flag_type flags =
175a83710eSEric Fiselier // regex_constants::match_default);
185a83710eSEric Fiselier
195a83710eSEric Fiselier #include <regex>
205a83710eSEric Fiselier #include <cassert>
21fd5ceb22SMarshall Clow #include "test_macros.h"
225a83710eSEric Fiselier
main(int,char **)23*2df59c50SJF Bastien int main(int, char**)
245a83710eSEric Fiselier {
255a83710eSEric Fiselier {
265a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
275a83710eSEric Fiselier const char phone_book[] = "555-1234, 555-2345, 555-3456";
285a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
295a83710eSEric Fiselier std::string("123-$&"));
305a83710eSEric Fiselier assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
315a83710eSEric Fiselier }
325a83710eSEric Fiselier {
335a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
345a83710eSEric Fiselier const char phone_book[] = "555-1234, 555-2345, 555-3456";
355a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
365a83710eSEric Fiselier std::string("123-$&"),
375a83710eSEric Fiselier std::regex_constants::format_sed);
385a83710eSEric Fiselier assert(r == "123-$555-1234, 123-$555-2345, 123-$555-3456");
395a83710eSEric Fiselier }
405a83710eSEric Fiselier {
415a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
425a83710eSEric Fiselier const char phone_book[] = "555-1234, 555-2345, 555-3456";
435a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
445a83710eSEric Fiselier std::string("123-&"),
455a83710eSEric Fiselier std::regex_constants::format_sed);
465a83710eSEric Fiselier assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
475a83710eSEric Fiselier }
485a83710eSEric Fiselier {
495a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
505a83710eSEric Fiselier const char phone_book[] = "555-1234, 555-2345, 555-3456";
515a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
525a83710eSEric Fiselier std::string("123-$&"),
535a83710eSEric Fiselier std::regex_constants::format_no_copy);
545a83710eSEric Fiselier assert(r == "123-555-1234123-555-2345123-555-3456");
555a83710eSEric Fiselier }
565a83710eSEric Fiselier {
575a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
585a83710eSEric Fiselier const char phone_book[] = "555-1234, 555-2345, 555-3456";
595a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
605a83710eSEric Fiselier std::string("123-$&"),
615a83710eSEric Fiselier std::regex_constants::format_first_only);
625a83710eSEric Fiselier assert(r == "123-555-1234, 555-2345, 555-3456");
635a83710eSEric Fiselier }
645a83710eSEric Fiselier {
655a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
665a83710eSEric Fiselier const char phone_book[] = "555-1234, 555-2345, 555-3456";
675a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
685a83710eSEric Fiselier std::string("123-$&"),
695a83710eSEric Fiselier std::regex_constants::format_first_only |
705a83710eSEric Fiselier std::regex_constants::format_no_copy);
715a83710eSEric Fiselier assert(r == "123-555-1234");
725a83710eSEric Fiselier }
73*2df59c50SJF Bastien
74*2df59c50SJF Bastien return 0;
755a83710eSEric Fiselier }
76