Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use wildcards with string::find?

Tags:

c++

wildcard

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?

like image 813
Chris Danger Gillett Avatar asked Nov 29 '25 04:11

Chris Danger Gillett


1 Answers

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

like image 69
nims Avatar answered Dec 01 '25 18:12

nims



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!