添加头文件 #include <regex>
char* RegexRtspCameraIp(const char* CameraIp)//从rtsp地址中截取IP地址(192.168.xx.xx)
{
char *strLine = nullptr;
int MatchState = -1;
std::string pattern{"[0-9].[0-9].[0-9].[0-9][^/,:]{1,}"};//[0-9].[0-9].[0-9].[0-9][^:]{1,10}
std::regex re(pattern);
std::string m_str(CameraIp);
//最初的字符串 未匹配前(包括554端口号以及通道号,次码流)
//rtsp://192.168.200.52:554/channel1/2
std::smatch results;
if(std::regex_search(m_str,results,re))
{
MatchState = 0;
strLine = new char [strlen(CameraIp) + 1];
std::string str1;
str1 = results.str();
strcpy(strLine,str1.c_str());
}else
{
LLERROR("[%d] regex_search Str False", MatchState);
}
return strLine;//192.168.200.xx
}
记得delete ...
//2017-0815修改了匹配的:
- std::string pattern{"(\\d{1,3})[.](\\d{1,3}[.](\\d{1,3})[.])(\\d{1,3})"};