gpt4 book ai didi

c++ - 如果包含字符串,如何删除整个句子

转载 作者:行者123 更新时间:2023-12-03 16:46:44 25 4
gpt4 key购买 nike

如果字符串包含模式,我需要从字符串中删除整个句子。
这里我有模式“链接”或“链接”,如果它存在于字符串中,我需要删除包含它的整个句子。

std::string subject = "This is previous sentence. This can be any sentences. Link 2.1.19.3 [Example]. This is can be any other sentence. This is next sentence.";   

std::string removeRedundantString(std::string subject)
{
std::string removeSee = subject;
std::smatch match;

std::regex redundantSee("(Link.*$)");

if (std::regex_search(subject, match, redundantSee))
{
removeSee = std::regex_replace(subject, redundantSee, "");
}
}
预期输出:
This is previous sentence. This can be any sentences.This is can be any other sentence. This is next sentence.
实际输出:
This is previous sentence. This can be any sentences.
由于使用了正则表达式 "(Link.*$)",上述实际输出即将到来删除从链接开始到字符串末尾的句子。
我无法弄清楚使用什么正则表达式来获得预期的输出。
以下是我需要测试的不同测试用例:
测试用例 1:
std::string subject = "Note this is second pattern, Ops that next the scheduler; link the amount for the full list of docs. The number of value varies from 0 to 4.";
输出: Note this is second pattern, Ops that next the scheduler;The number of value varies from 0 to 4.测试用例 2:
std::string subject = "This is another pattern. (Link Doc::78::hello::Core::mount). Since this patern includes non-numeric value.";
输出: This is another pattern.Since this patern includes non-numeric value.任何帮助,将不胜感激。

最佳答案

我会推荐

std::regex redundantSee(R"(\W*\b[Ll]ink\b(?:\d+(?:\.\d+)*|[^.])*[.?!])")
its online demo .注意原始字符串文字语法, R"(...)" .字符串模式可以简单地放在里面而不是 ...没有任何额外的转义。
正则表达式详细信息:
  • \W* - 零个或多个非单词字符
  • \b - 一个词边界
  • [Ll]ink - Linklink
  • \b - 一个词边界
  • (?:\d+(?:\.\d+)*|[^.])* - 零个或多个序列
  • \d+(?:\.\d+)* - 一个或多个数字后跟零个或多个 . 序列和一位或多位数字
  • | - 或
  • [^.] - 除 . 以外的任何字符

  • [.?!] - 一个 ? , .! .
  • 关于c++ - 如果包含字符串,如何删除整个句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66551590/

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