Reputation: 23092
I am trying to build a regex expression using boost::regex
(my version of GCC doesn't handle std::regex
particularly well).
I want to replace any occurrence of whitespaces with \s*
so that it'll match any number of whitespaces (I don't care if the number of whitespaces differs).
Here's an example:
std::string s = "this is a test string";
s = boost::regex_replace(s, boost::regex("\\s+"), "\\s*");
std::cout << s;
This produces:
thiss*iss*as*tests*string;
What I want is this\s*is\s*a\s*test\s*string
. Why isn't this working?
I tried replacing with \\\\s*
but that just produces this\\s*is\\s*a\\s*test\\s*string
, also not what I'm after!
Upvotes: 0
Views: 580
Reputation: 490398
You don't seem to gain a lot from using a regex in this case. With one (probably minor) proviso, it's pretty easy to do this without them (though matching the result against a string will pretty much require regexes).
std::string input = "this is a test string";
std::istringstream buffer(input);
std::ostringstream result;
std::copy(std::istream_iterator<std::string>(buffer),
std::istream_iterator<std::string>(),
infix_ostream_iterator<std::string>(result, "\\s*"));
std::cout << result.str();
Result: this\s*is\s*a\s*test\s*string
.
This uses the infix_ostream_iterator
from another post.
The proviso: this doesn't (currently) attempt to deal with whitespace at the beginning and/or end of the result pattern. In most cases, these aren't necessary, but if you want to add them, doing so is pretty trivial.
Upvotes: 2
Reputation: 23092
This isn't a very satisfying solution, but based on the comments I'm thinking this might be an issue with my version of boost
(1.53). To get around this issue I simply nuke all whitespaces:
stringToMatch = boost::regex_replace(stringToMatch, boost::regex("\\s+"), "");
expression = boost::regex_replace(expression, boost::regex("\\s+"), "");
This has the same effect, so I'm going with it for now, but it's certainly not ideal!
Upvotes: 0