Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8241597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:53:04+00:00 2026-06-07T20:53:04+00:00

I am trying to replace certain patterns in a string with different replacement patters.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:53:05+00:00Added an answer on June 7, 2026 at 8:53 pm

    The c++ (0x, 11, tr1) regular expressions do not really work (stackoverflow) in every case (look up the phrase regex on this page for gcc), so it is better to use boost for a while.

    You may try if your compiler supports the regular expressions needed:

    #include <string>
    #include <iostream>
    #include <regex>
    
    using namespace std;
    
    int main(int argc, char * argv[]) {
        string test = "test replacing \"these characters\"";
        regex reg("[^\\w]+");
        test = regex_replace(test, reg, "_");
        cout << test << endl;
    }
    

    The above works in Visual Studio 2012Rc.

    Edit 1: To replace by two different strings in one pass (depending on the match), I’d think this won’t work here. In Perl, this could easily be done within evaluated replacement expressions (/e switch).

    Therefore, you’ll need two passes, as you already suspected:

     ...
     string test = "test replacing \"these characters\"";
     test = regex_replace(test, regex("\\s+"), "_");
     test = regex_replace(test, regex("\\W+"), "");
     ...
    

    Edit 2:

    If it would be possible to use a callback function tr() in regex_replace, then you could modify the substitution there, like:

     string output = regex_replace(test, regex("\\s+|\\W+"), tr);
    

    with tr() doing the replacement work:

     string tr(const smatch &m) { return m[0].str()[0] == ' ' ? "_" : ""; }
    

    the problem would have been solved. Unfortunately, there’s no such overload in some C++11 regex implementations, but Boost has one. The following would work with boost and use one pass:

    ...
    #include <boost/regex.hpp>
    using namespace boost;
    ...
    string tr(const smatch &m) { return m[0].str()[0] == ' ' ? "_" : ""; }
    ...
    
    string test = "test replacing \"these characters\"";
    test = regex_replace(test, regex("\\s+|\\W+"), tr);   // <= works in Boost
    ...
    

    Maybe some day this will work with C++11 or whatever number comes next.

    Regards

    rbo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replace a certain character in a string with a space
I'm trying to replace the occurence of a certain String from a given text
I am trying to replace certain parts of the string below. '''<td align=center> 5
I cant get this to work. I am trying to replace certain characters when
I'm trying to use jQuery to replace all occurrences of a particular string that
I'm trying to replace only certain parts of a string via an SQL query.
Trying to replace a string in a properties file on a certain line by
I'm trying to replace certain characters with some other strings I got it to
I'm trying to replace certain text inside the div info when an image (which
Trying to replace any non alpha-numeric characters with a hyphen. Can't see why it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.