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 9168637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:40:21+00:00 2026-06-17T15:40:21+00:00

Is there a nice and elegant way (using boost::algorithm::replace maybe?) to replace all occurrences

  • 0

Is there a nice and elegant way (using boost::algorithm::replace maybe?) to replace all occurrences of a character in a string – unless preceded by a backslash?

so that

std::string s1("hello 'world'");
my_replace(s1, "'", "''"); // s1 becomes "hello ''world''"

std::string s2("hello \\'world'"); // note: only a single backslash in the string
my_replace(s2, "'", "''"); // s2 becomes "hello \\'world''"

Using boost::regex, this can be done using:

std::string my_replace (std::string s, std::string search, std::string format) {
  boost::regex e("([^\\\\])" + search);
  return boost::regex_replace(s, e, "\\1" + format);
}

But I prefer not to use boost::regex due to performance reasons. boost::algorithm::replace looks like a good fit, but I can’t figure out exactly how.

  • 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-17T15:40:23+00:00Added an answer on June 17, 2026 at 3:40 pm

    Here is a simple algorithm that does the job:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string replace(char c, string replacement, string s)
    {
        string chars = string("\\") + c;
    
        size_t pos = s.find_first_of(chars);
        while (pos != string::npos)
        {
            char& ch = s[pos];    
            if (ch == '\\')
            {
                pos = s.find_first_of(chars, pos + 2);
            }
            else if (ch == c)
            {
                s.replace(pos, 1, replacement);
                pos = s.find_first_of(chars, pos + replacement.length());
            }
        }
    
        return s;
    }
    
    int main()
    {
        cout << replace('\'', "''", "hello \\'world'");
    }
    

    UPDATE:

    Following @BenVoigt’s suggestion, I reformulated the algorithm to avoid in-place operations. That should bring further performance improvement:

    string replace(char c, string replacement, string const& s)
    {
        string result;
        size_t searchStartPos = 0;
    
        string chars = string("\\") + c;
        size_t pos = s.find_first_of(chars);
        while (pos != string::npos)
        {
            result += s.substr(searchStartPos, pos - searchStartPos);
            if (s[pos] == '\\')
            {
                result += string("\\") + c;
                searchStartPos = pos + 2;
            }
            else if (s[pos] == c)
            {
                result += replacement;
                searchStartPos = pos + 1;
            }
    
            pos = s.find_first_of(chars, searchStartPos);
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a nice way in the WinAPI to get a path relative to
Is there a nice way in Ext to move elements in DOM? I want
I want to swap both references, is there a nice way make such a
Is there a way in Entity Framework 4 (using CTP4 and Code First if
Is there a simple or elegant way to grab only the time of day
There are nice SO question and answers about this issue, but these options didn't
Are there any nice libraries to render text in an image for Java? Java
In Spring Source Toolsuite (Eclipse with some Spring tuning) there is nice wizard to
I got some binary files containing integers. Is there some nice Unix command, that
There's a nice F# workflow builder for Rx here: http://blogs.msdn.com/b/dsyme/archive/2011/05/30/nice-f-syntax-for-rx-reactive-extensions.aspx I've been trying to

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.