shomit
shomit

Reputation: 209

How to separate a string using separator

I want to separate this string using _ as the separators

{[Data1]_[Data2]_[Data3]}_{[Val1]_[Val2]}_{[ID1]_[ID2]_[ID3]} 

where underscore should not be considered inside the { } brackets.

so when we separate the strings we should have three new data items

 {[Data1]_[Data2]_[Data3]}
 {[Val1]_[Val2]}
 {[ID1]_[ID2]_[ID3]}

currently I was separating using boost

        std::vector<std::string> commandSplitSemiColon;
        boost::split(commandSplitSemiColon, stdstrCommand, boost::is_any_of("_"), 
        boost::token_compress_on);

but how can we ignore the underscore inside the { } brackets and only consider underscore which are not inside the brackets.

Upvotes: 1

Views: 129

Answers (2)

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

If you note the similarity between your format and conventional CSV (comma-separated value), you can search for existing solutions.

For example, on this forum: How can I read and parse CSV files in C++?. Specifically, using Boost Tokenizer with escaped_list_separator

Upvotes: 2

Haoliang
Haoliang

Reputation: 1264

A manual solution: iterate through the string and use a variable to check whether it's in the bracket or not:

std::vector<std::string> strings;
std::string s = "{[Data1]_[Data2]_[Data3]}_{[Val1]_[Val2]}_{[ID1]_[ID2]_[ID3]}";
std::string token = "";
bool inBracket = false;
for (char c : s) {
    if (c == '{') {
        inBracket = true;
        token += c;
    }
    else if (c == '}') {
        inBracket = false;
        token += c;
    }
    else if (c == '_' && !inBracket) {
        strings.push_back(token);
        token = "";
    }
    else {
        token += c;
    }
}

if (!token.empty()) {
    strings.push_back(token);
}

Upvotes: 2

Related Questions