gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:38:55 24 4
gpt4 key购买 nike

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

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

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

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

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 组匹配时,您需要将整个匹配粘贴回去,如果没有,则用 " 替换:

#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++ demo . John 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 " you are you doing " what are " you up to "

关于c++ - 在 C++ 中有选择地替换 std::string 中的 (") 双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46909477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com