gpt4 book ai didi

c++ - 获取 URL token 的正则表达式是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:40:31 33 4
gpt4 key购买 nike

假设我有这样的字符串:

bunch of other html<a href="http://domain.com/133742/The_Token_I_Want.zip" more html and stuffbunch of other html<a href="http://domain.com/12345/another_token.zip" more html and stuffbunch of other html<a href="http://domain.com/0981723/YET_ANOTHER_TOKEN.zip" more html and stuff

匹配 The_Token_I_Wantanother_tokenYET_ANOTHER_TOKEN 的正则表达式是什么?

最佳答案

RFC 2396 的附录 B给出了一个用于将 URI 拆分为其组件的正则表达式,我们可以根据您的情况对其进行调整

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*/([^.]+)[^?#]*)(\?([^#]*))?(#(.*))?
#######

这在 $6 中留下了 The_Token_I_Want,这是上面的“hashderlined”子表达式。 (请注意,哈希不是模式的一部分。)现场观看:

#! /usr/bin/perl

$_ = "http://domain.com/133742/The_Token_I_Want.zip";
if (m!^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*/([^.]+)[^?#]*)(\?([^#]*))?(#(.*))?!) {
print "$6\n";
}
else {
print "no match\n";
}

输出:

$ ./prog.plThe_Token_I_Want

UPDATE: I see in a comment that you're using boost::regex, so remember to escape the backslash in your C++ program.

#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
boost::regex token("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*"
"/([^.]+)"
// ####### I CAN HAZ HASHDERLINE PLZ
"[^?#]*)(\\?([^#]*))?(#(.*))?");

const char * const urls[] = {
"http://domain.com/133742/The_Token_I_Want.zip",
"http://domain.com/12345/another_token.zip",
"http://domain.com/0981723/YET_ANOTHER_TOKEN.zip",
};

BOOST_FOREACH(const char *url, urls) {
std::cout << url << ":\n";

std::string t;
boost::cmatch m;
if (boost::regex_match(url, m, token))
t = m[6];
else
t = "<no match>";

std::cout << " - " << m[6] << '\n';
}

return 0;
}

输出:

http://domain.com/133742/The_Token_I_Want.zip:  - The_Token_I_Wanthttp://domain.com/12345/another_token.zip:  - another_tokenhttp://domain.com/0981723/YET_ANOTHER_TOKEN.zip:  - YET_ANOTHER_TOKEN

关于c++ - 获取 URL token 的正则表达式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3489104/

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