3

I have been looking online without success for something that does the following. I have some ugly string returned as part of a Betfair SOAP response that uses a number of different char delimiters to identify certain parts of the information. What makes it awkward is that they are not always just one character length. Specifically, I need to split a string at ':' characters, but only after I have first replaced all instances of "\\:" with my personal flag "-COLON-" (which must then be replaced again AFTER the first split).

Basically I need all portions of a string like this

 "6(2.5%,11\:08)~true~5.0~1162835723938~"

to become

 "6(2.5%,11-COLON-08)~true~5.0~1162835723938~

In perl it is (from memory)

 $mystring =~ s/\\:/-COLON-/g; 

I have been looking for some time at the functions of std::string, specifically std::find and std::replace and I know that I can code up how to do what I need using these basic functions, but I was wondering if there was a function in the standard library (or elsewhere) that already does this??

3

2 Answers 2

5

boost::replace_all(input_string, "\\:", "-COLON-");

Sign up to request clarification or add additional context in comments.

1 Comment

Sometimes I wish the standard had stuff like this.
1

If you have C++11 something like this ought to do the trick:

#include <string>
#include <regex>

int main()
{
std::string str("6(2.5%,11\\:08)~true~5.0~1162835723938~");
std::regex rx("\\:"); 
std::string fmt("-COLON-");
std::regex_replace(str, rx, fmt);

return 0;
}

Edit: There is an optional fourth parameter for the type of match as well which can be anything found in std::regex_constants namespace I do believe. For example replacing only the first occurrence of the regular expression match with the supplied format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.