2011-08-08 208 views
0

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

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

非常感谢。

+1

看看这篇文章。 http://stackoverflow.com/questions/4716098/regular-expressions-in-c-stl – BrandonSun

回答

0

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

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

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

+0

这是用于C++/CLR。问题似乎要求本机C++解决方案。 – Xion

相关问题