繁体   English   中英

如何将通配符与string :: find一起使用?

[英]How can I use wildcards with string::find?

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

例如:

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

因此,如果我问“妈妈好吗?”,输出将是“妈妈做得很好”。

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

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

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

#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 fine\n";
    }
}

您可以轻松修改此想法以适合您的需求。 否则,请按照src给出的答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM