简体   繁体   English

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

[英]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. 我希望能够在string::find使用通配符,然后获取该通配符位置中的内容。

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? 如果我应该使用AIML,AIML可以执行.bat文件吗?

C++ 2011 provides a regular expression library . C ++ 2011提供了一个正则表达式库 If you can't use C++ 2011 yet you can use the Boost.Regex library or any C library like PCRE . 如果您仍不能使用C ++ 2011,则可以使用Boost.Regex库或任何C库,例如PCRE

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 否则,请按照src给出的答案

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

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