在C ++中的std :: string中有选择地替换(“)双引号 [英] Selectively replace (") doublequotes in a std::string in C++

查看:127
本文介绍了在C ++中的std :: string中有选择地替换(“)双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有选择地替换C ++ std :: string中的()双引号。
即我要替换字符串除外中所有出现的()双引号

I want to selectively replace the (") doublequotes in a C++ std::string. i.e. i want to replace all occurences of (") doublequotes in a string except the 1st and last occurence of (") doublequotes in the string.

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

Example- following code replaces ALL occurence of (") doublequotes

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

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

However, i don't want to replace the 1st and last occurenece of the string.


ie在字符串中,我不想在(Hello)&
之前替换()末尾的那个。

i.e. in the string i don't want to replace (") just before "Hello" & the one at the end.



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


推荐答案

方案1:引号前加空格/尾随空格前的引号


您可以使用正则表达式来捕获字符串开头/结尾的引号,以及将前导/尾随空格插入到组1中,并在所有其他情况下匹配引号。然后,您需要为每种可能性实现自定义替换:当第1组匹配时,您需要将整个匹配项粘贴回去;如果不匹配,请使用& quot; 替换:

#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 ++演示。通过 John Martin

您可以使用

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.

请参见 regex演示

C ++演示

#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& quot;你好吗?什么是& quot;您最多达到

这篇关于在C ++中的std :: string中有选择地替换(“)双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆