Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jmarrec opened this issue Nov 8, 2024 · 1 comment
Open

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

jmarrec opened this issue Nov 8, 2024 · 1 comment

Comments

@jmarrec
Copy link

jmarrec commented Nov 8, 2024

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
Copy link

pbs3141 commented Nov 19, 2024

See #250

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants