Chris Danger Gillett
Chris Danger Gillett

Reputation: 127

How can I use wildcards with string::find?

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?

Upvotes: 2

Views: 20492

Answers (2)

nims
nims

Reputation: 3871

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

Upvotes: 5

src
src

Reputation: 551

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.

Upvotes: 5

Related Questions