将字符串与regEx通配符值进行比较

时间:2011-08-08 13:38:57

标签: c++ regex

因此,我需要针对reg ex通配符值列表检查字符串(url),以查看是否存在匹配项。我将拦截HTTP请求并根据预先配置的值列表进行检查,如果匹配,请对URL执行某些操作。例子:

Request URL: http://www.stackoverflow.com

Wildcards: *.stackoverflow.com/
           *.stack*.com/
           www.stackoverflow.*

有没有适合C ++的好库?任何好的例子都会很棒。我看到的伪代码如下:

std::string requestUrl = "http://www.stackoverflow.com";
std::vector<string> urlWildcards = ...;

BOOST_FOREACH(string wildcard, urlWildcards) {
    if (requestUrl matches wildcard) {
        // Do something
    } else {
        // Do nothing
    }
}

非常感谢。

2 个答案:

答案 0 :(得分:0)

如果您使用VS 2010,请考虑使用c ++ tr1引入的正则表达式。

有关详细信息,请参阅以下页面。

http://www.johndcook.com/cpp_regex.html

答案 1 :(得分:0)

  

以下代码示例使用正则表达式查找精确的子字符串匹配项。搜索由静态IsMatch方法执行,该方法将两个字符串作为输入。第一个是要搜索的字符串,第二个是要搜索的模式。

#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;

int main()
{
    array<String^>^ sentence = 
        {
            "cow over the moon",
            "Betsy the Cow",
            "cowering in the corner",
            "no match here"
        };

    String^ matchStr = "cow";
    for (int i=0; i<sentence->Length; i++)
    {
        Console::Write( "{0,24}", sentence[i] );
        if ( Regex::IsMatch( sentence[i], matchStr,
                 RegexOptions::IgnoreCase ) )
            Console::WriteLine("  (match for '{0}' found)", matchStr);
        else
            Console::WriteLine("");
        }
        return 0;
    }
}

来自MSDN的代码(http://msdn.microsoft.com/en-us/library/zcwwszd7(v=vs.80).aspx)。

相关问题