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?

Yadollah
  • 77
  • 8

1 Answers1

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.

swestrup
  • 4,079
  • 3
  • 22
  • 33