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

Rat*_*esh 5 c++ regex c++11

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

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, "");
    }
}
Run Code Online (Sandbox Code Playgroud)

预期输出:

This is previous sentence. This can be any sentences.This is can be any other sentence. This is next sentence.
Run Code Online (Sandbox Code Playgroud)

实际输出:

This is previous sentence. This can be any sentences.
Run Code Online (Sandbox Code Playgroud)

上面的实际输出是因为使用了正则表达式"(Link.*$)",它删除了从 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.";
Run Code Online (Sandbox Code Playgroud)

输出: 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.";
Run Code Online (Sandbox Code Playgroud)

输出 : This is another pattern.Since this patern includes non-numeric value.

任何帮助,将不胜感激。

Wik*_*żew 3

我会推荐

std::regex redundantSee(R"(\W*\b[Ll]ink\b(?:\d+(?:\.\d+)*|[^.])*[.?!])")
Run Code Online (Sandbox Code Playgroud)

查看其在线演示。请注意原始字符串文字语法,R"(...)". 字符串模式可以简单地放在里面,而不...需要任何额外的转义。

正则表达式详细信息

  • \W*- 零个或多个非单词字符
  • \b- 单词边界
  • [Ll]ink-Linklink单词
  • \b- 单词边界
  • (?:\d+(?:\.\d+)*|[^.])*- 零个或多个序列
    • \d+(?:\.\d+)*- 一位或多位数字后跟零个或多个序列.以及一位或多位数字
    • |- 或者
    • [^.]- 除 a 之外的任何字符.
  • [.?!]- 一个?.!