在C ++中有选择地替换(")std :: string中的双引号

时间:2017-10-24 11:34:01

标签: c++ regex string c++11 regex-negation

我想有选择地替换C ++ std :: string中的(")双引号。 即我想替换字符串中的所有(")双引号的出现,除了字符串中(")双引号的第一个和最后一个出现。

示例 - 以下代码替换(")双引号的 ALL 出现

std::string str = "\"Hello people \" how are you doing \" what are \" you upto \"";
str = std::regex_replace(str, std::regex("\\\""), """);

但是,我不想替换字符串的第一个和最后一个出现。

  

即。在字符串中,我不想在#34; Hello"之前替换(") &安培;   最后一个。

std::string str = "\"Hello people \" how are you doing \" what are \" you upto \"";

2 个答案:

答案 0 :(得分:3)

场景1:在前导空格之后/尾随空格之前的引号

您可以使用正则字符串捕获字符串开头/结尾的引号以及前导/尾随空格到第1组,并且将匹配所有其他上下文中的引号。然后,您需要为每种可能性实现自定义替换:当组1匹配时,您需要将整个匹配粘贴回来,如果没有,则替换为"

#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 "&quot;";
  } 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;
}

请参阅C++ demoJohn Martin的回调实现。

场景2:字符串

的开头/结尾处的引号

您可以使用

std::regex("(?!^)\"(?!$)")

如果在开始时找到(?!^)"),则^否定前瞻会导致匹配失败,如果在最后找到(?!$)则失败$ #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("(?!^)\"(?!$)"), "&quot;"); std::cout << str << std::endl; return 0; } 1}})字符串。

请参阅regex demo

C++ demo

"Hello people &quot; how are you doing &quot; what are &quot; you upto "

输出:sort_bubble

答案 1 :(得分:1)

没有正则表达式和提升的解决方案

  auto aPos1 = aString.find_first_of("\"");
  auto aPos2 = aString.find_last_of("\"");
  auto aPos = aString.length() - 1;
  for( ; aPos > aPos1 ; aPos--)
  {
    auto aVal = aString.at(aPos);
    if(aPos != aPos2 && aPos != aPos1)
    {
       if(aVal == '\"')
       {
        aString.erase(aPos,1);
       }
    }
  }