c++ - 正则表达式? - 海峡发现? - C++ - Windows 7 - 尝试在 pong irc 上回答

标签 c++ windows

我有一个字符串(来自 irc 服务器的答案)。

我需要从这个字符串中获取整数,以回答 (PONG number_from_sv)

some txt PING :i_need_this_numbers some_text

如何让这个数字接近“PING :”,我不知道它有多长。我只知道,那是一个数字?

最佳答案

使用 c++11 标准,您可以使用内置正则表达式查找 ID。

一个可能的正则表达式是 PING :(\\d+),其中\d 屏蔽任意数字。 + 表示大于或等于 1(位数)。

查找 ID 的小脚本可能如下所示

#include <string>
#include <regex>
#include <iostream>    

using namespace std;



int main ()
{
    std::string s ("some txt PING :665454 some_text");
    std::smatch mt;
    std::regex r ("PING :(\\d+) ");

    if (std::regex_search ( s, mt, r))
    {
        smatch::iterator it = mt.begin()+1; // First match is entire s
        cout<<"Your ping ID is: "<<*it<<endl;
    }
    return 0;
}

关于c++ - 正则表达式? - 海峡发现? - C++ - Windows 7 - 尝试在 pong irc 上回答,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19328225/

相关文章:

c++ - 为什么socket在gdb下会失败?

python - Windows 文件方案 URI 上的 urlparse() 在开始时留下额外的斜杠

c++ - 如何在 C++ 中获取以下输入

c++ - 在bigint类的+和*上进行运算符重载时,在丢弃限定符时出现错误

c++ - OpenGL 2D 纹理无法正确显示 C++

windows - 如何使用包含空格的字符串检查环境变量的值

Java 将 JPanel 高度设置为窗口高度的百分比

c++ - 如何捕获从 Windows 服务启动的 session 注销

C++ libdl.so : Can't open shared library in 32bit application

c++ - ubuntu 中是否有库的默认路径?