gpt4 book ai didi

c++ - 如何获取匹配组的索引

转载 作者:行者123 更新时间:2023-11-30 05:01:17 28 4
gpt4 key购买 nike

我想列出输入中的所有匹配组。例如,我想在 User-Agent HTTP header 中打印我关心的所有浏览器。一场比赛就够了。有没有办法摆脱下面代码中的内部 for 循环。我查看了 boost/regex/sub_match.hpp,但我没有任何想法。

#include <string> 
#include <iostream>
#include <boost/regex.hpp> // Boost 1.59, no C++14 for me

enum BrowserType
{
FIREFOX = 0 ,
CHROME,
SAFARI,
OPERA,
IE,
EDGE,
OTHER,
};

const boost::regex BROWSERS_REGEX("(Firefox)|(Chrome)|(Safari)|(Opera)|(MSIE)|(Edge)|(Trident)");

int main()
{
// I expect two matches here CHROME and SAFARI
std::string input("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/62.0.3202.94");
boost::sregex_iterator res(input.begin(), input.end(), BROWSERS_REGEX);
boost::sregex_iterator end;
for(; res != end; ++res)
{
// elude copy here ?
boost::smatch what = *res;
// Can I know the index of the matching group w/o 'for'?
for (int type = 0;type < OTHER;type++)
{
int groupIndex = type+1;
if (what[groupIndex].matched)
std::cout << (BrowserType)type << ",";
}
std::cout << "\n";

}

return 0;
}

重要更新。基于 strstr() 的代码是处理小于 1K 字节的字符串的最快方法

    const char *input("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/62.0.3202.94");
const char *browsers[OTHER+1] = {"Firefox", "Chrome", "Safari", "Opera", "MSIE", "Edge", "Trident"};
int i = 0;
for (const char **browser = &browsers[0];browser <= &browsers[OTHER];browser++, i++)
{
if (strstr(input, *browser))
{
groupindex = i;
}
}

最佳答案

您可以使用命名捕获。

我今天早些时候用过它:While doing url encoding , The std::regex_replace doesn't work properly for character "+" . Can some body help me out?

不过,在你的情况下,我会使用 qi::symbols:

struct browser_type_sym : boost::spirit::qi::symbols<char, BrowserType> {
browser_type_sym() {
this->add
("Firefox", FIREFOX)
("Chrome", CHROME)
("Safari", SAFARI)
("Opera", OPERA)
("MSIE", IE)
("Edge", EDGE)
("Trident", OTHER);
}
} static const browser_type;

您可以简单地将它与 BrowserType 的任何容器一起使用:

template <typename Types>
bool extract_browser_ids(std::string const& userAgent, Types& into) {
using boost::spirit::repository::qi::seek;
return parse(userAgent.begin(), userAgent.end(), *seek [ browser_type ], into);
}

使用vector<BrowserType>

如您所见,如果您使用 vector<BrowserType它将保留顺序和重复项:

Live On Coliru

int main() {
for (std::string const input : {
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/62.0.3202.94",
"Chrome; Opera; Chrome again!"
}) {
std::vector<BrowserType> types;
extract_browser_ids(input, types);
for(auto type : types)
std::cout << type << ",";

std::cout << "\n";
}
}

打印:

2,1,
1,3,1,

使用set<BrowserType>

使用 set<> 时它将排序并删除重复数据:

Live On Coliru

    std::set<BrowserType> types;

打印:

1,2,
1,3,

不区分大小写

只是为了展示一些小的调整:

struct browser_type_sym : boost::spirit::qi::symbols<char, BrowserType> {
browser_type_sym() {
this->add
("firefox", FIREFOX)
("chrome", CHROME)
("safari", SAFARI)
("opera", OPERA)
("msie", IE)
("edge", EDGE)
("trident", OTHER);
}
} static const browser_type;

template <typename Types>
bool extract_browser_ids(std::string const& userAgent, Types& into) {
using boost::spirit::repository::qi::seek;
using boost::spirit::qi::no_case;
return parse(userAgent.begin(), userAgent.end(), *seek [ no_case [ browser_type ] ], into);
}

现在大小写无关紧要: Live On Coliru

备选方案: boost Xpressive

同样,您可以使用 Boost Xpressive,它稍微接近 regex方法(尽管 internally it still builds a trie of strings from the map )。

语义 Action 需要更多的体力劳动,使其不那么通用(与已经展示的灵气方法不同,如果不进行更改,它就无法与 std::set 一起使用)。

然而,为了完整性:

Live On Coliru

std::map<std::string, BrowserType> s_browser_type_map {
{"Firefox", FIREFOX},
{"Chrome", CHROME},
{"Safari", SAFARI},
{"Opera", OPERA},
{"MSIE", IE},
{"Edge", EDGE},
{"Trident", OTHER},
};

#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>

template <typename Types>
void extract_browser_ids(std::string const& userAgent, Types& into) {
using namespace boost::xpressive;

placeholder<Types> _result;
sregex type = (a1 = s_browser_type_map) [ push_back(_result, a1) ];

for (sregex_iterator it(userAgent.begin(), userAgent.end(), type, let(_result=into)),
end; it != end; ++it)
{ } // all side-effects in the semantic action
}

它打印与 vector<> 相同的输出 spirit 范例:

2,1,
1,3,1,

关于c++ - 如何获取匹配组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50373263/

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