c++ - 如何提取字符串中的整数?

标签 c++

我得到一行类似:"1001", Name

我想知道如何在没有 atoi 的情况下获取引号之间的数字。

问题要求让函数只获取字符串中两个引号之间的整数,然后获取名称并将其放入字符串中,但我不明白那部分。

最佳答案

使用正则表达式搜索:

#include <regex>
#include <iostream>

int main()
{
    const std::string s = "\"1001\", John Martin";
    std::regex rgx("\"(\\d+)\", *([\\w ]+)"); // this will extract quoted numbers in any string
    std::smatch match;

    if (std::regex_search(s.begin(), s.end(), match, rgx))
        std::cout << "ID: " << match[1] << ", Name: " << match[2] << '\n';
}

关于c++ - 如何提取字符串中的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56087576/

相关文章:

python - 导出函数时boost python模板参数推导/替换失败

c++ - 为什么我们从 MultiByte 转换为 WideChar?

c++ - 是什么导致 fprintf 出现 C++ 缓冲区溢出错误?

php - 使用 PHP 编译 C++ 文件

c++ - 在嵌入式环境中替代 boost::shared_ptr

c++ - gdb: 在 0x2aaaaaaab000 添加的符号文件系统提供的 DSO 中找不到可加载的部分

c++ -::write() 和::read() 尚未声明为错误。 Qt5 UNIX 信号处理

c++ - fastcgipp < 没有输出 utf8 字符

c++ - 挂其他问题?

c++ - 搜索遍历多个列表的项目