Scenario 1: Quotes after leading whitespace/before trailing whitespace
You may use a regex that will capture quotes at the start/end of string together with leading/trailing whitespace into Group 1, and will match quotes in all other contexts. Then you need to implement custom replacement for each of the possibilities: when Group 1 matches, you need to paste the whole match back, if not, replacr with "
:
#include <iostream>
#include <cstdlib>
#include <string>
#include <regex>
using namespace std;
template<class BidirIt, class Traits, class CharT, class UnaryFunction>
std::basic_string<CharT> regex_replace(BidirIt first, BidirIt last,
const std::basic_regex<CharT,Traits>& re, UnaryFunction f)
{
std::basic_string<CharT> s;
typename std::match_results<BidirIt>::difference_type
positionOfLastMatch = 0;
auto endOfLastMatch = first;
auto callback = [&](const std::match_results<BidirIt>& match)
{
auto positionOfThisMatch = match.position(0);
auto diff = positionOfThisMatch - positionOfLastMatch;
auto startOfThisMatch = endOfLastMatch;
std::advance(startOfThisMatch, diff);
s.append(endOfLastMatch, startOfThisMatch);
s.append(f(match));
auto lengthOfMatch = match.length(0);
positionOfLastMatch = positionOfThisMatch + lengthOfMatch;
endOfLastMatch = startOfThisMatch;
std::advance(endOfLastMatch, lengthOfMatch);
};
std::sregex_iterator begin(first, last, re), end;
std::for_each(begin, end, callback);
s.append(endOfLastMatch, last);
return s;
}
template<class Traits, class CharT, class UnaryFunction>
std::string regex_replace(const std::string& s,
const std::basic_regex<CharT,Traits>& re, UnaryFunction f)
{
return regex_replace(s.cbegin(), s.cend(), re, f);
}
std::string my_callback(const std::smatch& m) {
if (m.str(1).length() % 2 == 0) {
return """;
} else {
return m.str(0);
}
}
int main() {
std::string str = "\"Hello people \" how are you doing \" what are \" you upto \"";
cout << regex_replace(str, regex("(^\\s*\"|\"\\s*$)|\""), my_callback) << endl;
return 0;
}
See the C++ demo. The callback implemenation by John Martin.
Scenario 2: Quotes at the start/end of string
You may use
std::regex("(?!^)\"(?!$)")
The (?!^)
negative lookahead fails the match if the "
is found at the start (^
) and (?!$)
fails if it is found at the end ($
) of string.
See the regex demo
C++ demo:
#include <iostream>
#include <regex>
using namespace std;
int main() {
std::string str = "\"Hello people \" how are you doing \" what are \" you upto \"";
str = std::regex_replace(str, std::regex("(?!^)\"(?!$)"), """);
std::cout << str << std::endl;
return 0;
}
Output: "Hello people " how are you doing " what are " you upto "