Home > Back-end >  How to separate a string using separator
How to separate a string using separator

Time:06-26

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.

CodePudding user response:

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);
}

CodePudding user response:

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

  •  Tags:  
  • c
  • Related