如何在 string::find 中使用通配符? [英] How can I use wildcards with string::find?

查看:56
本文介绍了如何在 string::find 中使用通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to be able to use a wildcard in string::find and then fetch what was in that wildcard place.

例如:

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

所以如果我问,妈妈怎么样?",输出将是妈妈做得很好."

And so if I ask, "How is Mom doing?", the output would be "Mom is doing fine."

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

What libraries would I use for this, or how would I write the code manually? If I should use AIML, can AIML execute .bat files?

推荐答案

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

Regarding your question on how to do it manually, i will give you an idea with this simple code, which solves the example which you gave in the question.

#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

You can easily modify this idea to suit your needs. Else, follow the answer given by src

这篇关于如何在 string::find 中使用通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆