gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:51:48 25 4
gpt4 key购买 nike

因此,我需要根据 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
}
}

非常感谢。

最佳答案

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

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

关于c++ - 将字符串与 regEx 通配符值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6982971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com