在開發HTTP相關程序時,經常會碰到從網絡鏈接URL中提取協議名、服務器、路徑等目標對象,如果使用C/C++字符串操作函數,那么則顯得有點麻煩且代碼不易維護,其實關于文本內容的解析工作,都可優先考慮使用正則表達式庫來解決處理,C++方面的正則庫也有很多種,如atl、pcre、boost。下面就使用boost中的regex來解析URL提取協議名、服務器、路徑為目標說明其用法。
協議名 可有可無,如果有時則后面必跟著://,如果沒有,則默認為使用http協議。通常還有其它的協議如https、ssl、ftp、mailto等。因此匹配協議名的正則表達式應該是(?:(mailto|ssh|ftp|https?)://)?,注意這個表達式本身捕獲了協議名,但不包括://。
服務器 或是域名,如www.csdn.net;或是IP地址,如192.168.1.1,可帶端口號,如192.168.1.1:8080。匹配域名的正則表達式為(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|net|edu|biz|gov|org|in(?:t|fo)|(?-i:[a-z][a-z])),表達式"(?:com|net|edu|biz|gov|org|in(?:t|fo)"匹配了com、net、edu、biz、gov、org、int、info等常見的域名,而(?-i:[a-z][a-z])匹配了國家代碼,而且只允許小寫為合法的,如www.richcomm.com.cn。匹配IP要盡量精確,考慮到IP每部分應為數字且范圍在0-255之間,因此表達式應為(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])。注意以上域名或IP的正則式本身不捕獲它們,這是為了留在后面作為整體捕獲。
端口號的正則表達式為(?::(\d{1,5}))?,這里限制了端口號為1至5位的數字,更精確的匹配如要求在某范圍如[1024,65535]間則可參考以上IP正則模式。綜上所得,匹配服務器的正則表達式為((?:(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|net|edu|biz|gov|org|in(?:t|fo)|(?-i:[a-z][a-z]))|(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])))(?::(\d{1,5}))?,這個正則式作為整體捕獲了域名或IP,及端口號(若有),如www.csdn.net,則得到www.csdn.net和空(沒有端口,http默認為80,https默認為443)子串;192.168.1.1:8080則得到192.168.1.1和8080子串。
路徑 最簡單的形式為(/.*)?,更精確的形式為/[^.!,?;"'<>()\[\]{}\s\x7F-\xFF]*(?:[.!,?]+[^.!,?;"'<>()\[\]{}\s\x7F-\xFF]+)*。
以上所有正則表達式均為ascii字符集,對于unicode字符集則在其前加L即可。
為方便使用,封裝成了兩個自由模板函數,如下所示
1
template<typename charT>
2
inline bool boost_match(const charT* pattern,const charT* text,unsigned int flags=boost::regex::normal,boost::match_results<const charT*>* result=NULL)
3

{
4
boost::basic_regex<charT,boost::regex_traits<charT> > expression(pattern,flags);
5
if(NULL==result)
6
return boost::regex_match(text,expression);
7
return boost::regex_match(text,*result,expression);
8
}
9
10
template<typename charT>
11
inline bool boost_search(const charT* pattern,const charT* text,unsigned int flags=boost::regex::normal,boost::match_results<const charT*>* result=NULL)
12

{
13
boost::basic_regex<charT,boost::regex_traits<charT> > expression(pattern,flags);
14
if(NULL==result)
15
return boost::regex_search(text,expression);
16
return boost::regex_search(text,*result,expression);
17
}
測試示例如下
1
static const string protocol = "(?:(mailto|ssh|ftp|https?)://)?";
2
static const string hostname = "(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\\.)+(?:com|net|edu|biz|gov|org|in(?:t|fo)|(?-i:[a-z][a-z]))";
3
static const string ip = "(?:[01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.(?:[01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.(?:[01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.(?:[01]?\\d\\d?|2[0-4]\\d|25[0-5])";
4
static const string port = "(?::(\\d{1,5}))?";
5
static const string path = "(/.*)?";
6
static const string pattern = protocol + "((?:" + hostname + "|" + ip + "))" + port + path;
7
8
int _tmain(int argc, _TCHAR* argv[])
9

{
10
using namespace boost;
11
12
//形式1: 帶協議名,服務器為名稱,不帶端口號
13
bool ret;
14
string text = "http://www.shnenglu.com/qinqing1984";
15
boost::cmatch what;
16
ret=boost_match(pattern.c_str(),text.c_str(),regex::icase|regex::perl,&what);
17
assert(ret);
18
assert(what[1].str()=="http");
19
assert(what[2].str()=="www.shnenglu.com");
20
assert(what[3].str()=="");
21
assert(what[4].str()=="/qinqing1984");
22
23
//形式2: 不帶協議名,服務器為名稱,帶端口號
24
text = "www.shnenglu.com:80/qinqing1984";
25
ret=boost_match(pattern.c_str(),text.c_str(),regex::icase|regex::perl,&what);
26
assert(ret);
27
assert(what[1].str()=="");
28
assert(what[2].str()=="www.shnenglu.com");
29
assert(what[3].str()=="80");
30
assert(what[4].str()=="/qinqing1984");
31
32
//形式3: 不帶協議名,服務器為名稱,不帶路徑
33
text = "www.shnenglu.com:80";
34
ret=boost_match(pattern.c_str(),text.c_str(),regex::icase|regex::perl,&what);
35
assert(ret);
36
assert(what[1].str()=="");
37
assert(what[2].str()=="www.shnenglu.com");
38
assert(what[3].str()=="80");
39
assert(what[4].str()=="");
40
41
//形式4: 協議為https,服務器為IP,帶端口號
42
text = "https://192.168.1.1:443/index.html";
43
ret=boost_match(pattern.c_str(),text.c_str(),regex::icase|regex::perl,&what);
44
assert(ret);
45
assert(what[1].str()=="https");
46
assert(what[2].str()=="192.168.1.1");
47
assert(what[3].str()=="443");
48
assert(what[4].str()=="/index.html");
49
50
//形式5: 端口超過5位數
51
text = "ftp://192.168.1.1:888888";
52
ret=boost_match(pattern.c_str(),text.c_str(),regex::icase|regex::perl,&what);
53
assert(!ret);
54
55
//形式6: 沒有協議名
56
text = "//192.168.1.1/index.html";
57
ret=boost_match(pattern.c_str(),text.c_str(),regex::icase|regex::perl,&what);
58
assert(!ret);
59
60
//形式7: 沒有服務器
61
text = "http:///index.html";
62
ret=boost_match(pattern.c_str(),text.c_str(),regex::icase|regex::perl,&what);
63
assert(!ret);
64
65
//形式8: 不合法的服務器
66
text = "cppblog/index.html";
67
ret=boost_match(pattern.c_str(),text.c_str(),regex::icase|regex::perl,&what);
68
assert(!ret);
69
70
return 0;
71
}
對URL的解析,因時間有限,本文所述不盡詳細,只是略作分析,以點帶面,更多的精確匹配則依賴于實際的應用需求。
posted on 2011-11-27 17:22
春秋十二月 閱讀(7929)
評論(5) 編輯 收藏 引用 所屬分類:
Opensrc