如何将通配符与字符串::查找一起使用

How can I use wildcards with string::find?

本文关键字:查找 一起 字符串 通配符      更新时间:2023-10-16

我希望能够在string::find中使用通配符,然后获取该通配符位置的内容。

例如:

if (string::npos !=input.find("How is * doing?")
{
     cout<<"(the wildcard) is doing fine."<<endl;
}

因此,如果我问"妈妈过得怎么样?",输出将是"妈妈做得很好"。

我将为此使用哪些库,或者我将如何手动编写代码?如果我应该使用 AIML,AIML 可以执行.bat文件吗?

关于您关于如何手动执行此操作的问题,我将通过这个简单的代码为您提供一个想法,它解决了您在问题中给出的示例。

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string expr="How is * doing?";
    string input="How is Mom doing?";
    int wildcard_pos=expr.find("*");
    if(wildcard_pos!=string::npos)
    {
        int foo=input.find(expr.substr(0,wildcard_pos)),bar=input.find(expr.substr(wildcard_pos+1));
        if(foo!=string::npos && bar!=string::npos)
        cout<<input.substr(wildcard_pos,bar-wildcard_pos)<<" is doing finen";
    }
}

您可以轻松修改此想法以满足您的需求。否则,请按照src给出的答案进行操作

C++ 2011 提供了一个正则表达式库。如果你不能使用C++ 2011,你可以使用Boost.Regex库或任何C库,如PCRE。