hanickadot / compile-time-regular-expressions

Compile Time Regular Expression in C++
https://twitter.com/hankadusikova
Apache License 2.0
3.38k stars 187 forks source link

[Question] Is there an equivalent to regex_replace? #332

Open jmarrec opened 3 weeks ago

jmarrec commented 3 weeks ago

I was wondering if there's a facility such as https://en.cppreference.com/w/cpp/regex/regex_replace in CTRE?

So far I was only able to come up with something not great

static constexpr auto pattern = ctll::fixed_string{" "};

std::string str = get_string();

std::string result;
bool first = true;
for (auto match : ctre::split<pattern>(str)) {
    if (!first) {
        result += "_";
    } else {
        first = false;
    }
    result += std::string{match.get<0>()};
}
fmt::print("{}\n", result);

https://godbolt.org/z/9PMzef97v

A better version but that would require C++26 (join_with is C++23, and ranges::to is C++26): https://godbolt.org/z/9vcTWePGe

pbs3141 commented 1 week ago

See https://github.com/hanickadot/compile-time-regular-expressions/issues/250