我得到一行类似:"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/