What would a regexp be for a code below that captures all the numbers in m:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
std::smatch m;
std::string line, reg;
line = "12365,45236,458,1,999,96332"; // any number of integers
reg = "???????"; // the question
std::regex e(reg);
std::regex_search(line, m, e);
if(m.size() > 0) {
for(unsignedint i = 0; i < m.size(); i++) {
// m.str(i) here contains "12365", "45236", "458", ...
}
}
#include <iostream>
#include <regex>
#include <string>
#include <vector>
int main()
{
const std::string text = "12365,45236,458,1,999,96332, +345 , -568" ;
// [\+\-]? - an optional + or - sign followed by \d+ - one or more decimal digits (greedy)
const std::regex integer_re( "[\\+\\-]?\\d+" ) ; // note: \ needs to be escaped, so '\\'
std::vector<int> seq ;
for( std::sregex_iterator iter( text.begin(), text.end(), integer_re ) ; iter != std::sregex_iterator() ; ++iter )
{
seq.push_back( std::stoi( iter->str() ) ) ; // may throw if number is too big
std::cout << seq.back() << '\n' ;
}
}