简体   繁体   English

C ++在字符串中查找特定数字

[英]C++ find specific number in a string

I'm quite new to regular expressions. 我对正则表达式很陌生。 I have this string. 我有这个字符串。

string s = media_type=video|key_frame=1|pkt_pts=1516999|pkt_pts_time=50.566633|pkt_dts=1516999|

I need to get 50.566633 value extracted using string operators and regular expressions in C++. 我需要使用C ++中的字符串运算符和正则表达式来提取50.566633值。 Can some one suggest a way to do this? 有人可以建议一种方法吗?

Regex is well worth studying because it is so useful. 正则表达式非常有用,因为它非常有用。

This works for me: 这对我有用:

#include <regex>
#include <iostream>

std::string s = "media_type=video|key_frame=1|pkt_pts=1516999|pkt_pts_time=50.566633|pkt_dts=1516999|";

int main()
{
    // parens () define a capture group to extract your value
    // so the important part here is ([^|]*)
    // - capture any number of anything that is not a |
    std::regex rx("pkt_pts_time=([^|]*)");

    std::smatch m;
    if(std::regex_search(s, m, r))
        std::cout << m.str(1); // first captured group
}

Click on Working Example 单击工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM