正则匹配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;
}

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