用另一个词替换字符串中的单个词

Replacing single word in a string with another word

努力寻找一种方法将 "he" 替换为 "he or she"、"his with "his or hers",而不用 "the or she" 替换 "the"我的代码如下:

#include <iostream>
#include <string>

using namespace std;

void myReplace(string& str, const string& oldStr, const string& newStr)
{
    if (oldStr.empty())
    {
        return;
    }

    for (size_t pos = 0; (pos = str.find(oldStr, pos)) != string::npos;)
    {
        str.replace(pos, oldStr.length(), newStr);
        pos += newStr.length();
    }
}

int main()
{
    string searchStr;

Beginning:

    cout << "Please enter a sentence (Maximum of 100 characters)\n"
         << "Or type 'exit' to close the program\n";
    getline(cin, searchStr);

    cout << "\nYour input:\n\t" << searchStr;

    myReplace(searchStr, "he", "he or she");
    cout << "\nReplaced Text\n\t" << searchStr << "\n\n";

    goto Beginning;
}

我的程序做什么...

Input: He is the man
Output: He or she is the or she man

它应该做什么...

Input: He is the man
Output: He or she is the man

任何人都可以帮助我解决这个问题。 如果您要问...是的,我到处都搜索了 Google。完全不符合我的需要。 提前致谢

有多种方法可以实现你想要做的事情,通过继续你已经拥有的东西,为了让它发挥作用,你将拥有:(快速注意,这将是概念或伪代码,没有'我已经好几年没用过 C++了)

  1. 快速而肮脏的方法:

当您尝试匹配一个词时,如您所说,如果该词包含 he,它将被替换,因此:the 变为 the or she

要解决这个问题,您需要考虑 ussually(稍后详细介绍)在单词前后出现的内容。通常是白色space。这意味着一个快速的解决方法是替换“he”而不是 "he"。 所以像The something he something这样的句子确实会给我们The something he or she something

但正如其他人所说,当句子以您要替换的内容开头时,这会导致问题。这就是为什么您要添加 space before and after 您的初始句子。

假设"He is something he"作为我们的句子,这将变成“他是他”,允许替换工作。然后在最后修剪字符串将摆脱多余的 spaces。 所以你将拥有:

searchStr = " " + searchStr + " ";   
myReplace(searchStr, " he ", " he or she ");
trim(searchStr)
  1. 制作单词列表(向量)然后替换它们

首先我们假设一个词是由 something between two white spaces 定义的,由于多种原因,这在本质上是错误的:

  • 句子的first/last字不会starting/ending加上space。
  • 最后一个单词可能以标点符号结尾,例如 .!,这在前面的示例中不起作用
  • 字符串中的标点符号:he, him and her 无效
  • he/her这样的特殊标志将再次失效。

在这种情况下,我们想要做的是使用包含可能分割单词的特殊字符的正则表达式 (Regex in C++) 来分割单词。在这里,您可能想要做的事情有很多可能性。

  • 您可能希望通过拆分所有特殊字符来分隔单词(取决于您的使用方式,您最终可能会丢失汉字等)
  • 您可能想要创建一个拆分对象列表:,: ;_.!?/~'" 等等。

所以在做了这样的事情之后(伪):

ourString = "He, is mean to the teacher!"
delimiter = "[ ,.!?]".toRegex //whitespace and some punctuation marks
list = split(ourString, delimiter)

列表将是:[He, is, mean, to, the, teacher](注意,我们将丢失标点符号,稍后会详细介绍)

现在我们可以简单地遍历列表,用我们需要的替换每个元素并将其连接回来:

string = ""
for(word in list)
   string+= if(word.toLowerCase == "he") " he or she " else " " word " "

现在我们将有 " He or she is mean to the teacher "(同样,标点符号丢失)

如果我们想保留标点符号怎么办?

如果我们想使用相同的方法,而不是简单地拆分标点符号本身,我们可以使用更复杂的正则表达式 (an example in python)。完整正则表达式的另一种替代方法是:

  • 先遍历字符串,在标点符号前后添加spaces
  • 通过仅在白色上拆分将其拆分为列表 spaces
  • 更换过程
  • 把绳子放回原处
string = "He, is !mean."
regex = "[,!.:;]"
string = replace(string, regex with " it ") 
//the string is now: "He ,  is  ! mean . " 
// something to get rid of multiple spaces and make them into a single one
normliseWhiteSpaces(string) 
delimiter = " " 
list = split(string, delimiter) //the list is now [he, ,, is, !, mean, .]
string = ""
for(word in list)
    string+= if(word.toLowerCase == "he") " he or she " else " " word " "
//the string is now "He or she , is mean . " so we need to: 
normliseWhiteSpaces(string)
trim(string)
  1. 其他完全取决于您的实际目标是什么,您对源数据的期望是什么,等等。
  2. 但我不想要正则表达式...(那么Read the duplicate comment