I'm just learned the basics of Regular Expressions a few days ago, so please forgive me if I make some obvious mistakes.
I want
regex_replace
to match the whole expression, but only replace part of it. For example, I want to replace all "!"s with "?"s, but only if there is one letter or more before the "!", so if the input's "hello, world!" the output would be "hello, world?", but if the input is "hello, world !" it would remain the same.
This is what I have so far:
1 2 3 4 5 6
|
int main() {
std::regex r("[A-Za-z]+(!)");
std::string target("hello, world!");
std::cout << std::regex_replace(target, r, "?") << std::endl;
return 0;
}
|
But the problem is
regex_replace
ignores my capture groups and just replaces the whole thing, so I get "hello, ?" as the output. Is there a way I can make it only replace the "!", but match the whole sequence?
(Actually, I have made a calculator, and want to make it accept things like "x!". So I was thinking, with regex, can you replace "x!" with "fact(x)"? If yes, can you show me the code to do so? Any help would be appreciated. Thanks.)