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, ST, SA>
135a83710eSEric Fiselier // regex_replace(const basic_string<charT, ST, SA>& s,
145a83710eSEric Fiselier // const basic_regex<charT, traits>& e, const charT* fmt,
155a83710eSEric Fiselier // regex_constants::match_flag_type flags =
165a83710eSEric Fiselier // regex_constants::match_default);
175a83710eSEric Fiselier
185a83710eSEric Fiselier #include <regex>
195a83710eSEric Fiselier #include <cassert>
20fd5ceb22SMarshall Clow #include "test_macros.h"
215a83710eSEric Fiselier
main(int,char **)22*2df59c50SJF Bastien int main(int, char**)
235a83710eSEric Fiselier {
245a83710eSEric Fiselier {
255a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
265a83710eSEric Fiselier std::string phone_book("555-1234, 555-2345, 555-3456");
275a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
285a83710eSEric Fiselier "123-$&");
295a83710eSEric Fiselier assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
305a83710eSEric Fiselier }
315a83710eSEric Fiselier {
325a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
335a83710eSEric Fiselier std::string phone_book("555-1234, 555-2345, 555-3456");
345a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
355a83710eSEric Fiselier "123-$&",
365a83710eSEric Fiselier std::regex_constants::format_sed);
375a83710eSEric Fiselier assert(r == "123-$555-1234, 123-$555-2345, 123-$555-3456");
385a83710eSEric Fiselier }
395a83710eSEric Fiselier {
405a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
415a83710eSEric Fiselier std::string phone_book("555-1234, 555-2345, 555-3456");
425a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
435a83710eSEric Fiselier "123-&",
445a83710eSEric Fiselier std::regex_constants::format_sed);
455a83710eSEric Fiselier assert(r == "123-555-1234, 123-555-2345, 123-555-3456");
465a83710eSEric Fiselier }
475a83710eSEric Fiselier {
485a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
495a83710eSEric Fiselier std::string phone_book("555-1234, 555-2345, 555-3456");
505a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
515a83710eSEric Fiselier "123-$&",
525a83710eSEric Fiselier std::regex_constants::format_no_copy);
535a83710eSEric Fiselier assert(r == "123-555-1234123-555-2345123-555-3456");
545a83710eSEric Fiselier }
555a83710eSEric Fiselier {
565a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
575a83710eSEric Fiselier std::string phone_book("555-1234, 555-2345, 555-3456");
585a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
595a83710eSEric Fiselier "123-$&",
605a83710eSEric Fiselier std::regex_constants::format_first_only);
615a83710eSEric Fiselier assert(r == "123-555-1234, 555-2345, 555-3456");
625a83710eSEric Fiselier }
635a83710eSEric Fiselier {
645a83710eSEric Fiselier std::regex phone_numbers("\\d{3}-\\d{4}");
655a83710eSEric Fiselier std::string phone_book("555-1234, 555-2345, 555-3456");
665a83710eSEric Fiselier std::string r = std::regex_replace(phone_book, phone_numbers,
675a83710eSEric Fiselier "123-$&",
685a83710eSEric Fiselier std::regex_constants::format_first_only |
695a83710eSEric Fiselier std::regex_constants::format_no_copy);
705a83710eSEric Fiselier assert(r == "123-555-1234");
715a83710eSEric Fiselier }
72*2df59c50SJF Bastien
73*2df59c50SJF Bastien return 0;
745a83710eSEric Fiselier }
75