I am trying to replace certain patterns in a string with different replacement patters.
Example:
string test = "test replacing "these characters"";
What I want to do is replace all ' ' with '_' and all other non letter or number characters with an empty string. I have the following regex created and it seems to tokenize correctly, but I am not sure how to (if possible) perform a conditional replace using regex_replace
.
string test = "test replacing "these characters"";
regex reg("(\s+)|(\W+)");
expected result after replace would be:
string result = "test_replacing_these_characters";
EDIT: I cannot use boost, which is why I left it out of the tags. So please no answer that includes boost. I have to do this with the standard library. It may be that a different regex would accomplish the goal or that I am just stuck doing two passes.
EDIT2:
I did not remember what characters were included in w
at the time of my original regex, after looking it up I have further simplified the expression. Again the goal is anything matching s+ should be replaced with '_' and anything matching W+ should be replaced with empty string.