How can I use wildcards with string::find?

两盒软妹~` 提交于 2019-12-23 03:14:38

问题


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

For example:

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."

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?


回答1:


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";
    }
}

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




回答2:


C++ 2011 provides a regular expression library. If you can't use C++ 2011 yet you can use the Boost.Regex library or any C library like PCRE.



来源:https://stackoverflow.com/questions/11276000/how-can-i-use-wildcards-with-stringfind

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!