#include <algorithm>
#include<boost/algorithm/string.hpp>
#include<boost/regex.hpp>
using namespace std;
using namespace boost;
string _getBasehtttp(string url)
{
regex exrp( "^(?:http://)?([^\\/]+)(.*)$" );
match_results<string::const_iterator> what;
if( regex_search( url, what, exrp ) )
{
string base( what[1].first, what[1].second );
return base;
}
return "";
}
int main( ) {
cout << _getBasehtttp("httpasd://www.google.co.in");
}
如果我输入http://www.google.co.in
我将以www.google.com
的身份返回,但如果我输入httpasd://www.google.co.in
我将收到httpasd
..我应该没有任何匹配比赛???
答案 0 :(得分:2)
http://
不匹配,但是它是可选的,所以没问题; “一个或多个不斜杠的字符”匹配httpasd:
,当然.*
匹配后面的所有内容,从斜杠(包括)开始。对于任何常见的正则表达式实现,这都会以相同的方式工作,没有特定于c ++的具体内容!
答案 1 :(得分:0)
^(?:http://)?([^\\/]+)(.*)$
?在(?:http://)?
的末尾表示该位是可选的
此([^\\/]+)
捕获并匹配任何不是\或/
的内容
这个(.*)
捕获到行尾的所有其他内容
也许你的事情更像是
^(?:https?://)([^\\/]+)(.*)$
可能会考虑
的完整网址语法 file:// /C:/temp/app/example.html
file:// C : /temp/app/example.html
file:// C : \temp\app\example.html
http://user@www.hotmail.passport.login.net:8080/test/url.htm?view=smart
[method][ server ][ path ][optional]
[user][ domain ][port]
然后你的正则表达式更像是
([a-zA-Z][a-zA-Z0-9\\+\\-\\.]*://)?(([^@/\\\\]+@)?([a-zA-Z0-9_'!~\\-,;&=\\.\\$\\*\\(\\)\\+]+)(:\\d*)?)?([/\\\\][^?]*)?(\\?.*)?