正則匹配URL中的IP地址,端口號和文件名

公司有個需求,要讀取URL中的IP地址,端口號傳給接口函數中,再獲取文件名寫入一下,網上沒太找到合適的正則,而且發現,這種東西還是自己寫舒服一些

#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main () {
   string str ("http://10.10.114.152:2392/eciiwce/oiweoi/a.xml");
    regex str_expr ("^http[s]?://([0-9.]*):(\\d*).*/(.*)$");

   smatch sm;
   regex_match (str,sm,str_expr);
    
   if(regex_match ( str.cbegin(), str.cend(), sm, str_expr))
   {
	   cout << "String:range, size:" << sm.size() << " matches\n";

	   for(int i=1;i<(int)sm.size();++i)///<這個0是整個字符串,從開始遍歷是匹配全部的
		 cout<<sm[i]<<endl;
   }
   else
	 cout<<"not match"<<endl;
   return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章