2022-08-27 一个正则表达式处理RTSP鉴权的小例子

  • 分享一个小例子,代码供参考;不能直接拿来用!
// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <regex>
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <chrono>
#include <map>

using namespace std;


void ParseAuthenticateInfo(string test,map<string, string>& keyValues)
{
    const std::string strValueReg = "\"(.*)\"";
    smatch result;
  
    int nPos = test.find("Authenticate:");
    if (nPos != string::npos) {
        string subStr = test.substr(nPos + string("Authenticate:").length(), test.length() - nPos);
        //按逗号分割
        stringstream subStrSplit(subStr);
        string temp;
        vector<string>v;
        //获取接口
        while (getline(subStrSplit, temp, ',')) {
            v.push_back(temp);
        }
        //提取值
        for (auto& it : v) {
            nPos = it.find("=");
            if (nPos != string::npos) {
                string strLeft = it.substr(0, nPos);
                string strRight = it.substr(nPos + 1, it.length() - nPos);


                regex reg_Realm(strValueReg);
                if (regex_search(strRight, result, reg_Realm)) {
                     keyValues[strLeft] = result[1].str();           
                }

            }
        }
    }

}



int main()
{
    
    
    string test = "WWW-Authenticate: Digest realm=\"IP Camera(C7120)\", nonce=\"d4af7cb16c273d93c77ccbbcc27165dc\", stale=\"FALSE\"\r\n";
    map<string, string> keyValues;
    ParseAuthenticateInfo(test, keyValues);
 
#if 0  
    smatch result;
    
    const std::string strRealm = "Digest realm = \"(.*)\,";
    regex reg_Realm(strRealm);
  
    if (regex_search(test, result, reg_Realm))
    {
        for (int i = 1; i < result.size(); i++)
        {

        }
    }

    string a = "rtsp://(.*):(.*)@(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3}):(\\d+)";
    //string sName = "rtsp://admin:admin123@172.21.35.99:554/Streaming/Channels/102";
    string sName = "rtsp://admin:admin123@172.21.35.99:554/Streaming/Channels/102";
    regex reg_containport("(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3}):(\\d+)");
    if (regex_search(sName, result, reg_containport))
    {
        //包含ip和端口
        for (int i = 1; i < result.size(); i++)
        {
        }
        string a = result.prefix().str() + "aaaaaaaaaa" + result.suffix().str();
    }
    else
    {
        regex reg0("(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})");
        if (regex_search(sName, result, reg0))
        {
            //包含ip和端口
            for (int i = 1; i < result.size(); i++)
            {

            }
        }
    }
    std::cout << "Hello World!\n";
#endif
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ShaYQ

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值