开发者

Comparing a string with a regEx wildcard value

开发者 https://www.devze.com 2023-03-26 11:29 出处:网络
So I need to check a string (url) against a list of reg ex wildcard values to see if there is a match.I will be intercepting an HTTP request and checking it against a list of pre-configured values and

So I need to check a string (url) against a list of reg ex wildcard values to see if there is a match. I will be intercepting an HTTP request and checking it against a list of pre-configured values and if there is a match, do something to the URL. Examples:

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

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

Are there any good libraries for C++ for doing this? Any good examples would be great. Pseudo-code that I have looks something like:

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

Thanks a lot.


The following code example uses regular expressions to look for exact substring matches. The search is performed by the static IsMatch method, which takes two strings as input. The first is the string to be searched, and the second is the pattern to be searched for.

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

Code from MSDN (http://msdn.microsoft.com/en-us/library/zcwwszd7(v=vs.80).aspx).


If you use VS 2010, consider use the regex introduced by c++ tr1.

Refer to following page for more details.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消