wpdkawopdj
wpdkawopdj

Reputation: 1

The integer token that I need is behind characters so istringstream is not tokenising it

int integers;
std::list<int> integersList = {};
string token;

while(iss >> token)
{
    if(stringstream(token) >> integers)
    {
        integersList.push_back(integers);
    }
}

One of the tokens I need to parse is

U<sub>54778</sub><br

The istringstream doesn’t tokenise the integer inside of it, it only splits in along the spaces.

All the other integer tokens in the string are separated by spaces but this one is not.

Upvotes: 0

Views: 70

Answers (1)

A M
A M

Reputation: 15277

This will most probably work with a stringstream in an easy way.

As written in the comments, it is better to use a "regex" for this. Very fortunately C++ has a build-in "regex" library. You may read here about it.

And even better, there is an iterator, with which you can iterate over all patterns in a std::string: the std::sregex_token_iterator.

This gives you very powerful possibilities, because you can match many different patterns by a regex. Please read about it here.

With that, you can come up with a very simple program like the below:

#include <iostream>
#include <string>
#include <list>
#include <regex>
#include <algorithm>

// Simple Regex for optional sign and digits
const std::regex re{ "[+-]?\\d+" };

int main() {
    // The test string
    std::string test{ "U<sub>54778</sub><br+123ddd 4 -55 66" };

    // Here we will store our integers
    std::list<int> integersList{};

    // Get all integers
    std::transform(std::sregex_token_iterator(test.begin(), test.end(), re), {}, std::back_inserter(integersList), [](const std::string& s) { return std::stoi(s); });

    // Show debug output
    for (const int i : integersList) std::cout << i << ' ';
}

Please note: If you need to validate the correct format or range for an integer, then the regex will get more complicated.

Upvotes: 0

Related Questions