How to extract all full numbers from a string in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <regex>
#include <vector>

std::vector<std::string> extract_full_numbers(const std::string& s) {
    std::regex re("\\d+");
    std::smatch match;
    std::vector<std::string> numbers;

    std::string temp = s;
    while (std::regex_search(temp, match, re)) {
        numbers.push_back(match.str());
        temp = match.suffix();
    }

    return numbers;
}

int main() {
    std::string s = "c&c++28$c#.net&^java*() 1901 php <>/python 3.14.7";

    auto nums = extract_full_numbers(s);

    for (const auto& n : nums)
        std::cout << n << " ";
}

    
    
/*
run:

28 1901 3 14 7 
    
*/
 

 



answered Sep 16 by avibootz

Related questions

1 answer 352 views
1 answer 278 views
1 answer 269 views
1 answer 276 views
1 answer 284 views
...