简体   繁体   中英

How to extract specific elements from a string?

I am trying to extract the first numbers from each block of numbers from the next string.

string s = "f 1079//2059 1165//2417 1164//2414 1068//1980";

In this example I need to extract 1079, 1165, 1164 and 1068

I have tried with getline and substr but I have not been able to.

You can utilize the <regex> (C++ regular expression library) with pattern (\\\\d+)// . Locate the numbers before double slashes. Also using the parentheses to extract the numbers only by submatch.

Here is usage.

string s = "f 1079//2059 1165//2417 1164//2414 1068//1980";

std::regex pattern("(\\d+)//");
auto match_iter = std::sregex_iterator(s.begin(), s.end(), pattern);
auto match_end = std::sregex_iterator();

for (;match_iter != match_end; match_iter++) 
{
    const std::smatch& m = *match_iter;
    std::cout << m[1].str() << std::endl;   // sub-match for token in parentheses, the 1079, 1165, ...
                                            // m[0]: whole match, "1079//"
                                            // m[1]: first submatch, "1070"
}

I usually reach for istringstream for this kind of thing:

std::string input = "f 1079//2059 1165//2417 1164//2414 1068//1980";
std::istringstream is(input);
char f;
if (is >> f)
{
    int number, othernumber;
    char slash1, slash2;
    while (is >> number >> slash1 >> slash2 >> othernumber)
    {
        // Process 'number'...
    }
}

here is an attempt with getline and substring which works.

auto extractValues(const std::string& source)
-> std::vector<std::string>
{
    auto target = std::vector<std::string>{};
    auto stream = std::stringstream{ source };
    auto currentPartOfSource = std::string{};
    while (std::getline(stream, currentPartOfSource, ' '))
    {
        auto partBeforeTheSlashes = std::string{};
        auto positionOfSlashes = currentPartOfSource.find("//");
        if (positionOfSlashes != std::string::npos)
        {
            target.push_back(currentPartOfSource.substr(0, positionOfSlashes));
        }
    }
    return target;
}

Or there is another split way to extract tokens, but it may involve some string copy.

Consider a split_by function like

std::vector<std::string> split_by(const std::string& str, const std::string& delem);

Possible implementations in Split a string in C++?

Make string be splitted by first, then splitted by // and extract first item.

std::vector<std::string> tokens = split_by(s, " ");
std::vector<std::string> words;
std::transform(tokens.begin() + 1, tokens.end(),  // drop first "f"              
               std::back_inserter(words), 
               [](const std::string& s){ return split_by(s, "//")[0]; });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM