【问题标题】:Selectively replace (") doublequotes in a std::string in C++在 C++ 中选择性地替换 std::string 中的 (") 双引号
【发布时间】:2018-04-05 04:32:21
【问题描述】:

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

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

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

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

即在字符串中我不想在 "Hello" & 之前替换 (") 最后一个。

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

【问题讨论】:

  • @WiktorStribiżew 实际上,您应该将其发布为答案
  • 如果你不能假设有 >= 2 个双引号,你应该先检查一下,例如与std::string::find_first_ofstd::string::find_last_of。或者只是在输入上声明先决条件而不违反它们
  • 字符串可以是R"(some text before" text inside with possible " in it " some text after)"吗?还是第一个/最后一个引号是第一个/最后一个字符?
  • 所以你有问题。你会想“我知道,我可以用正则表达式解决这个问题!”。现在你有两个问题:你最初的问题和正则表达式。
  • 您使用> 引用您的部分问题令人困惑。你在引用某人的话吗?

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


【解决方案1】:

场景 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("(?!^)\"(?!$)")

如果在字符串的开头 (^) 找到 ",则 (?!^) 否定前瞻失败匹配,如果在字符串的结尾 ($) 找到 (?!$),则匹配失败。

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("(?!^)\"(?!$)"), "&quot;");
    std::cout << str << std::endl;
    return 0;
}

输出:"Hello people &amp;quot; how are you doing &amp;quot; what are &amp;quot; you upto "

【讨论】:

  • 谢谢。我赞成这个。但是,它仍然没有回答我的确切问题,因为它必须跳过 (") 的第一次出现和 (") 的最后一次出现。但是,如果我一开始就有空格,这个正则表达式就不起作用。 like :: std::string str = " \"Hello \"people \" 你好吗\" what are \" you upto \""; - -- 这里有一个空格 - " \"H 与 "\"H 没有空格。
  • 你真的想用正则表达式来做吗? std::regex?有了 Boost,这会容易得多。
  • 要求不要使用 boost 而只能使用标准 c++ 库。
【解决方案2】:

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

  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);
       }
    }
  }

【讨论】:

  • 第一个和最后一个 " 只有在字符串的开头/结尾用 0+ 个空格分隔时才应保留。该方案不适用于当前的 OP 场景。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-20
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
相关资源
最近更新 更多