0

I want to put space between punctuations and other words in a sentence. But boost::regex_replace() replaces the punctuation with space, and I want to keep a punctuation in the sentence! for example in this code the output should be "Hello . hi , "

regex e1("[.,]");
std::basic_string<char> str = "Hello.hi,";
std::basic_string<char> fmt = " ";
cout<<regex_replace(str, e1, fmt)<<endl;

Can you help me?

4

1 回答 1

1

You need to use a replacement variable in your fmt string. If I understand the documentation correctly, then in the absence of a flags field, you'll want to use a Boost-Extended format string.

In that sub-language, you use $& to mean whatever was matched, so you should try defining fmt as:

std::basic_string<char> fmt = " $& ";

That should change each punctuation into that same character, surrounded by spaces.

于 2010-06-02T07:32:55.730 回答