• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-159  評論-223  文章-30  trackbacks-0
               在開發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即可。
               
               為方便使用,封裝成了兩個自由模板函數,如下所示
             1template<typename charT>
             2inline 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
            10template<typename charT>
            11inline 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}
               
               測試示例如下      
             1static const string protocol = "(?:(mailto|ssh|ftp|https?)://)?";
             2static 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]))";
             3static 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])";
             4static const string port = "(?::(\\d{1,5}))?";
             5static const string path = "(/.*)?";
             6static const string pattern = protocol + "((?:" + hostname + "|" + ip + "))" + port + path;
             7
             8int _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 春秋十二月 閱讀(7930) 評論(5)  編輯 收藏 引用 所屬分類: Opensrc

            評論:
            # re: 使用boost regex解析URL 2011-11-27 23:08 | guest
            最后一個是合法的,最常見的情況就是在在局域網內,有臺叫cppblog的機器,直接用cppblog訪問。  回復  更多評論
              
            # re: 使用boost regex解析URL 2011-11-28 08:57 | 萬連文
            如果程序僅限win平臺,使用InternetCrackUrl;否則的話請移植chrome等開源的解析,否則完備性不夠的話,后面隱藏的BUG會讓你瘋狂。  回復  更多評論
              
            # re: 使用boost regex解析URL 2011-11-28 09:29 | 春秋十二月
            你說的有道理,我明白@萬連文
              回復  更多評論
              
            # re: 使用正則表達式解析URL 2014-07-17 03:03 | lixubin
            linux 下編譯能通過,但是表達式好像有問題

            'boost::bad_expression'
            what(): Invalid preceding regular expression
            Aborted (core dumped)  回復  更多評論
              
            # re: 使用正則表達式解析URL 2015-08-11 14:35 | mz
            无码AV波多野结衣久久| 亚洲中文字幕久久精品无码APP| 潮喷大喷水系列无码久久精品 | 国产综合免费精品久久久| 看全色黄大色大片免费久久久| 看久久久久久a级毛片| 少妇久久久久久被弄到高潮| 国产精品无码久久久久久| 国产午夜电影久久| 一本一道久久精品综合| 亚洲va久久久噜噜噜久久天堂| 国产精品岛国久久久久| 亚洲午夜久久久久久噜噜噜| 51久久夜色精品国产| 久久精品人人做人人爽电影 | 久久久久亚洲AV片无码下载蜜桃| 欧美丰满熟妇BBB久久久| 热综合一本伊人久久精品| 久久久无码精品亚洲日韩蜜臀浪潮| 亚洲精品乱码久久久久久不卡| 99久久精品国内| 99久久国产综合精品女同图片| 久久99亚洲综合精品首页| av无码久久久久久不卡网站| 国产精品中文久久久久久久 | 少妇久久久久久被弄高潮| 热综合一本伊人久久精品| 久久久久国产一级毛片高清版| 秋霞久久国产精品电影院| 久久精品无码专区免费青青| 伊人久久大香线蕉综合影院首页| 精品综合久久久久久88小说| AV色综合久久天堂AV色综合在| 亚洲AV日韩AV永久无码久久| 久久久久亚洲AV片无码下载蜜桃| 人妻无码精品久久亚瑟影视| 久久久久久亚洲精品不卡| 久久se精品一区精品二区国产| 丁香五月网久久综合| 99热都是精品久久久久久| 情人伊人久久综合亚洲|