c++ 正则表达的一个应用

本文介绍了一个用于从RTSP地址中提取IP地址的C++函数。该函数利用正则表达式来精确匹配并分离出IP地址部分,适用于监控摄像头等设备的RTSP流地址。
添加头文件 #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})"};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Teleger

你的支持是我前进的方向

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值