PCRECPP(pcre)从url代码中提取主机名问题 [英] PCRECPP (pcre) extract hostname from url code problem
问题描述
我在c ++中有这段简单的代码:
I have this simple piece of code in c++:
int main(void)
{
string text = "http://www.amazon.com";
string a,b,c,d,e,f;
pcrecpp::RE re("^((\\w+):\\/\\/\\/?)?((\\w+):?(\\w+)?@)?([^\\/\\?:]+):?(\\d+)?(\\/?[^\\?#;\\|]+)?([;\\|])?([^\\?#]+)?\\??([^#]+)?#?(\\w*)");
if(re.PartialMatch(text, &a,&b,&c,&d,&e,&f))
{
std::cout << "match: " << f << "\n";
// should print "www.amazon.com"
}else{
std::cout << "no match. \n";
}
return 0;
}
当我运行它时没有找到匹配。
我确信regex模式是正确的,我的代码是什么问题。
When I run this it doesn't find a match. I pretty sure that the regex pattern is correct and my code is what's wrong. If anyone familiar with pcrecpp can take a look at this Ill be grateful.
编辑:
感谢 Dingo ,它工作的很好。
另一个问题是,结果是在第六位 - f。
我编辑了上面的代码,以便您可以复制/粘贴。
Thanks to Dingo, it works great.
another issue I had is that the result was at the sixth place - "f".
I edited the code above so you can copy/paste if you wish.
推荐答案
是你的代码包含 ??(
这是C ++中的 [
三字母或做某事来打破它们:
The problem is that your code contains ??(
which is a trigraph in C++ for [
. You'll either need to disable trigraphs or do something to break them up like:
pcrecpp::RE re("^((\\w+):\\/\\/\\/?)?((\\w+):?(\\w+)?@)?([^\\/\\?:]+):?(\\d+)?(\\/?[^\\?#;\\|]+)?([;\\|])?([^\\?#]+)?\\??" "([^#]+)?#?(\\w*)");
这篇关于PCRECPP(pcre)从url代码中提取主机名问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!